PDA

View Full Version : Question about C



ShadowGandalf
19th February 2007, 10:59 AM
if you're using fgets to read a line, and you want to (sometimes) keep reading the same line repeatedly, what is the easiest way to do this?

snoop
19th February 2007, 03:29 PM
fclose and re fopen the file.

ShadowGandalf
19th February 2007, 06:48 PM
wouldn't rewind() do the same thing as what you suggest? I just ended up using fgetpos and fsetpos.

Carnage
19th February 2007, 07:40 PM
if i'm right, that wouldn't reload the file into memory so if the reason for rereading the same line is to see if it gets changed, it wont work. You would need to reopen the file for that.

snoop
20th February 2007, 09:50 AM
A lot of OSes won't let you have multiple file handles opened on the same file, which is why it's better to close it if you're looking for changes.

ShadowGandalf
20th February 2007, 11:02 AM
if i'm right, that wouldn't reload the file into memory so if the reason for rereading the same line is to see if it gets changed, it wont work. You would need to reopen the file for that.

Well, i'm not looking for that, so that's good... it was more like, you kept rereading the line, continually expanding a buffer until you could hold the entire line.

snoop
20th February 2007, 11:53 AM
oh in that case...
just use fgets until you encounter \n, and expand your character pointer per character (then copy the contents of your first temp pointer with strcpy)