DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   help about fread and fwrite (http://www.daniweb.com/forums/thread159038.html)

Whilliam Nov 23rd, 2008 3:59 am
help about fread and fwrite
 
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;
 }

ArkM Nov 23rd, 2008 10:44 am
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:
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:
printf("\nFile saved\n");

Avoid using of non-standard function flushall().

Clockowl Nov 23rd, 2008 2:26 pm
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.

ArkM Nov 23rd, 2008 6:47 pm
Re: help about fread and fwrite
 
Quote:

Originally Posted by Clockowl (Post 742819)
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 ;)...

Clockowl Nov 23rd, 2008 6:51 pm
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. :)

Whilliam Nov 24th, 2008 7:17 am
Re: help about fread and fwrite
 
Quote:

Originally Posted by ArkM (Post 742713)
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:
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:
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..


All times are GMT -4. The time now is 2:45 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC