Reputation: 401
For example if the file contains:
12345
-3445654
1245646
I want to read the first line into a string using fgets()
. Then I want to read the second line in too check if there is a '-'
in the first spot. If there is one, I will read the second line and strcat
it to the first line.
Then I want to read the thrid line using fgets()
again. This time when there is no '-'
I just want to make the file go back to the beginning of the third line so that the next time I call fgets()
it will read the same third line again.
Is there a way I can do this?
Upvotes: 0
Views: 1961
Reputation: 47923
Whenever I want to
what I usually do is declare a second line-holding variable
char prevline[whateversize];
and then, somewhere between step 1 and step 2
strcpy(prevline, line);
(Naturally you have to be sure that the line
and prevline
variables are consistently allocated, e.g. as arrays of the same size, so that overflow isn't a problem.)
Upvotes: 0
Reputation: 25752
Generally you would just keep the part of the file just read, in the memory until you are sure you don't need it anymore.
Or you could read the entire file into a buffer and then jump around it using pointer as much as you like.
Or if you really must, you can more the current stream position with fseek, and then re-read the parts you need.
Upvotes: 1
Reputation: 2513
Use fgetc to read the first character on the next line, and if it's not a '-' use ungetc to put it back.
Upvotes: 1