c - filePointer automatically progressing in while loop? -
i getting myself fileusage in c. found code explaining reading strings file. curious how mechanically works.
while (!feof(fpointer)){ fgets(singleline, 150, fpointer); puts(singleline); }
how filepointer know @ end of loop go further line? shouldnt filepointer increased @ end of while loop?
cheers
a file
object stores information open file, including "current positition". each read/write operation increments current position how many bytes read/written. that's why can keep calling fgets
(or other input function) in loop , contents, chunk chunk.
similarly, can call printf
(or other output function) multiple times produce output, , won't overwrite each other.
you can , set current file position using ftell
, fseek
(subject restrictions, e.g. standard c doesn't require ftell
return actual byte offset on text files, "magic number" can passed fseek
(but i've never seen in real implementation)).
(your fpointer
points 1 file
object. shouldn't increment pointer; it'll point random memory.)
finally, while (!feof(fp))
wrong because feof
queries "end of file" bit stored in file
object. not predict whether next read fail due reaching end of file. "end of file" bit set true after input function reaches end of file. kind of loop run once more after end-of-file , process garbage (or stale, previous iteration) data.
Comments
Post a Comment