View Single Post
Join Date: Oct 2008
Posts: 61
Reputation: Whilliam is an unknown quantity at this point 
Solved Threads: 0
Whilliam's Avatar
Whilliam Whilliam is offline Offline
Junior Poster in Training

help about fread and fwrite

 
0
  #1
Nov 23rd, 2008
hello guys, Im trying to understand how fread and fwrite works. My teacher said that these functions can store/output binaries and texts in files. I know how to do it with string. Im confused with floats and integers.
I tried to make a program for it but Im getting errors. I can't run my program. Can anyone tell me whats wrong? Here is my code.
  1. int save(float d)
  2. {
  3. FILE *fp;
  4. char fn[50];
  5.  
  6. printf("What is the filename? ");
  7. flushall();
  8. gets(fn);
  9.  
  10. if((fp = fopen(fn, "wb")) == NULL)
  11. {
  12. printf("Cannot save file");
  13. return 0;
  14. }
  15.  
  16. fwrite(d, sizeof(float), 1, fp);
  17.  
  18. printf("\nFile saved");
  19. fclose(fp);
  20. return 0;
  21. }
  22.  
  23. int load()
  24. {
  25. FILE *fp;
  26. float b;
  27. char fn[50];
  28.  
  29. printf("What is the filename? ");
  30. flushall();
  31. gets(fn);
  32.  
  33. fp = fopen(fn, "rb");
  34. fread(b, sizeof(float), 1, fp);
  35.  
  36. fclose(fp);
  37. printf("%.2f", b);
  38. return 0;
  39. }
Reply With Quote