| | |
help about fread and fwrite
Thread Solved
![]() |
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.
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.
C Syntax (Toggle Plain Text)
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; }
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:
With gets you program crashed if the file name length is greater than 49.
Don't forget to append carriage return:
Avoid using of non-standard function flushall().
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:
c Syntax (Toggle Plain Text)
fgets(fn,sizeof fn,sdtin);
Don't forget to append carriage return:
c Syntax (Toggle Plain Text)
printf("\nFile saved\n");
Avoid using of non-standard function flushall().
Last edited by ArkM; Nov 23rd, 2008 at 10:45 am.
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.
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.
•
•
•
•
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.
... •
•
•
•
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:
With gets you program crashed if the file name length is greater than 49.c Syntax (Toggle Plain Text)
fgets(fn,sizeof fn,sdtin);
Don't forget to append carriage return:
c Syntax (Toggle Plain Text)
printf("\nFile saved\n");
Avoid using of non-standard function flushall().
Last edited by Whilliam; Nov 24th, 2008 at 7:19 am.
![]() |
Similar Threads
- fwrite(size_t_count) (C)
- fread, fwrite issues (C)
- file input and output (C)
- Operating Systems assignment (C++)
Other Threads in the C Forum
- Previous Thread: rank of a matrix
- Next Thread: keil software
| Thread Tools | Search this Thread |
* adobe ansi api array binarysearch centimeter changingto char character cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax database directory feet fflush fgets file floatingpointvalidation fork frequency function givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux highest histogram homework i/o inches infiniteloop input interest intmain() iso keyboard kilometer km linked linkedlist linux linuxsegmentationfault list locate looping lowest match meter microsoft mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc program programming pyramidusingturboccodes read recv recvblocked repetition reversing scanf scheduling segmentationfault send single socketprograming socketprogramming stack standard string suggestions systemcall unix urboc user voidmain() wab whythiscodecausesegmentationfault win32api windows.h windowsapi






