hey,
Does anybody know how to reset getline, that is, to make it read from the beginning of the file again?
thanks
hey,
Does anybody know how to reset getline, that is, to make it read from the beginning of the file again?
thanks
Dmitry Kashlev
I don't know about fstreams, but if you're using FILE *'s then you can just rewind it by saying.
edit:Code:rewind (fp);
I found out how to do it with fstreams, just use seekg.
Code:fp.seekg (0, ios::beg);
Last edited by Crimpy; 08-10-2002 at 04:21 PM.
what is fp?
What header does rewind() belong to?
thanks
Dmitry Kashlev
fp is the FILE pointer.
rewind is in the cstdio header, seekg is in the fstream header.Code:FILE *fp = fopen (FILENAME, "r"); if (!fp){ cerr<<"The file didn't open"<<endl; exit (EXIT_FAILURE); }
'getline()' wouldn't be an appropriate choice when working with file data, so your question is somewhat ambiguous, however...
'getline()' and 'get()' are "destructive" reading processes.
Both must read and remove at least one character from the stream. If not, the stream failbit() is set and you've got problems to deal with.
rewind() and seekg(0, ios::beg) are both methods of repositioning the file pointer to the beginning of a file, just as Crimpy has mentioned.
(seekp(0, ios::beg) does the same as seekg(). The 'p' and 'g' are put() and get() designators - istream and ostream, respectively).
You might investigate these three functions: putback(), unget() and peek(). These don't apply to file data, but might be handy to have in your arsenal anyway.
In case the answer to your question got "glossed over" here, since you refer specifically to files, use read(). This function is non-destructive to the file data.
Also, when opening your file, make sure you don't use 'ios::trunc' unless you want to render this entire discussion moot.
-Skipper
"When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow
>>, getline(), get(), peek(), putback(), read(), seekg() are all istream methods/operators, and as such are inhereted by ifstreams and subsequently, fstreams. getline() and get() and >> all remove data from the stream buffer, not from the file. reading a file with getline() or get() will not destroy the file. Writing to a file using something other than the app mode will "destroy" the file, however.
elad,
Good info. I was thinking more in terms of a standard input stream regarding getline() and get() vice file data, but your clarification is appreciated, by me anyway.
-Skipper
"When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow