I don't know how to tell the computer to stop reading when the file is done. I was looking around online and one example had
but don't that gave me a seg fault. Can anyone clarify this for me?Code:while(!feof(file))
I don't know how to tell the computer to stop reading when the file is done. I was looking around online and one example had
but don't that gave me a seg fault. Can anyone clarify this for me?Code:while(!feof(file))
actually it reads into some space and returns weird numbers.
looking at this example confused me, could you explain why they did an fgets on buffer?
Code:while (fgets(buf, sizeof(buf), fp) != NULL)
Because that's how you read things in. fgets is, I mean.
no i understand fgets but I don't understand why they pass in buf and sizeOf(buf). I don't get how that has anything to do with the file size of junk1.txt
The idea, TaiL, is that:
"If the system is already at end of file when you call fgets, then the contents of the array are unchanged and a null pointer is returned." (GNU)
There could be a flaw in this method too if the EOF occurs before/without a newline.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
so is this basically the concept?
char nullChkr[50];
while (fgets(nullChkr, 50, fp) != NULL)
nullChkr would get overwritten with a NULL when fp is done. thus kicking me out of the loop?
No, nullchkr wouldn't get overwritten -- fgets would return NULL and not change the value of the argument.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
No. nullChkr will not be overwritten with NULL. nullChkr gets filled with bytes read from the file. If there is an EOF detected, you may not get the full 49 (+1 for the nul) bytes asked for... The string, however much of it, in nullChkr will be terminated by a nul in any case. fgets() returns the pointer to the string, or NULL - in which case nullChkr's contents would not be touched.