943,569 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 4118
  • C RSS
Nov 23rd, 2008
0

help about fread and fwrite

Expand Post »
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. }
Similar Threads
Reputation Points: 19
Solved Threads: 0
Junior Poster
Whilliam is offline Offline
110 posts
since Oct 2008
Nov 23rd, 2008
1

Re: help about fread and fwrite

Is it so hard job to correct obvious errors: you get compiler messages on wrong function arguments: fread and fwrite want POINTERS to target/source data areas (&d - not d, &b - not b))!

Use code tag properly:
[code=c]
source
[/code]

Never use dangerous gets function: it does not check target buffer size. Use fgets instead of gets:
  1. fgets(fn,sizeof fn,sdtin);
With gets you program crashed if the file name length is greater than 49.

Don't forget to append carriage return:
  1. printf("\nFile saved\n");

Avoid using of non-standard function flushall().
Last edited by ArkM; Nov 23rd, 2008 at 10:45 am.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Nov 23rd, 2008
0

Re: help about fread and fwrite

I'm more of a newb but feeling cocky as always:

Don't AVOID compiler specific functions, but be sure to find a cross-compiler way to do it as well, as in: find it, use it a couple of time, memorize it.

However, I think it's a definite plus if you also know compiler specific tweaks.
Last edited by Clockowl; Nov 23rd, 2008 at 2:26 pm.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Nov 23rd, 2008
0

Re: help about fread and fwrite

Click to Expand / Collapse  Quote originally posted by Clockowl ...
I'm more of a newb but feeling cocky as always:

Don't AVOID compiler specific functions, but be sure to find a cross-compiler way to do it as well, as in: find it, use it a couple of time, memorize it.

However, I think it's a definite plus if you also know compiler specific tweaks.
Well, repeat your admonitions when you will port 500000 LOC with compiler-specific calls in the new environment ...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Nov 23rd, 2008
0

Re: help about fread and fwrite

Nah I just meant to say that if you're sure that you'll only use it on your system, why not use those compiler specific functions? I think it's overkill to make basically everything you create cross platform, but if you're coding for your job then you should I guess... I'm a hobby coder.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Nov 24th, 2008
0

Re: help about fread and fwrite

Click to Expand / Collapse  Quote originally posted by ArkM ...
Is it so hard job to correct obvious errors: you get compiler messages on wrong function arguments: fread and fwrite want POINTERS to target/source data areas (&d - not d, &b - not b))!

Use code tag properly:
[code=c]
source
[/code]

Never use dangerous gets function: it does not check target buffer size. Use fgets instead of gets:
  1. fgets(fn,sizeof fn,sdtin);
With gets you program crashed if the file name length is greater than 49.

Don't forget to append carriage return:
  1. printf("\nFile saved\n");

Avoid using of non-standard function flushall().
thanks ArkM. Now I can do storing and getting data from files with integers and floats. Thanks for the tips too. I'm really slow. It's hard for me to solve obvious errors that I haven't encountered before..
Last edited by Whilliam; Nov 24th, 2008 at 7:19 am.
Reputation Points: 19
Solved Threads: 0
Junior Poster
Whilliam is offline Offline
110 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: preprocessor uint to unsigned int
Next Thread in C Forum Timeline: keil software





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC