RSS Forums RSS

help about fread and fwrite

Please support our C advertiser: Programming Forums
Thread Solved
Reply
Posts: 44
Reputation: Whilliam is an unknown quantity at this point 
Solved Threads: 0
Whilliam's Avatar
Whilliam Whilliam is offline Offline
Light Poster

help about fread and fwrite

  #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.
int save(float d)
 {
	  FILE *fp;
	  char fn[50];

	  printf("What is the filename? ");
	  flushall();
	  gets(fn);

	  if((fp = fopen(fn, "wb")) == NULL)
	  {
			printf("Cannot save file");
			return 0;
	  }

	  fwrite(d, sizeof(float), 1, fp);

	  printf("\nFile saved");
	  fclose(fp);
	  return 0;
 }

 int load()
 {
	  FILE *fp;
	  float b;
	  char fn[50];

          printf("What is the filename? ");
          flushall();
	  gets(fn);

	  fp = fopen(fn, "rb");
	  fread(b, sizeof(float), 1, fp);

	  fclose(fp);
	  printf("%.2f", b);
	  return 0;
 }
AddThis Social Bookmark Button
Reply With Quote  
Posts: 2,000
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 331
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: help about fread and fwrite

  #2  
Nov 23rd, 2008
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 9:45 am.
Reply With Quote  
Posts: 371
Reputation: Clockowl is on a distinguished road 
Solved Threads: 25
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: help about fread and fwrite

  #3  
Nov 23rd, 2008
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 1:26 pm.
Reply With Quote  
Posts: 2,000
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 331
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: help about fread and fwrite

  #4  
Nov 23rd, 2008
Originally Posted by Clockowl View Post
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 ...
Reply With Quote  
Posts: 371
Reputation: Clockowl is on a distinguished road 
Solved Threads: 25
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: help about fread and fwrite

  #5  
Nov 23rd, 2008
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.
Reply With Quote  
Posts: 44
Reputation: Whilliam is an unknown quantity at this point 
Solved Threads: 0
Whilliam's Avatar
Whilliam Whilliam is offline Offline
Light Poster

Re: help about fread and fwrite

  #6  
Nov 24th, 2008
Originally Posted by ArkM View Post
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 6:19 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Similar Threads
Other Threads in the C Forum
Views: 1373 | Replies: 5 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:36 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC