OK, I’m a C++ newbie, but I’m trying to learn.
I have a question. I just finished reading a file using ifstream, and now I want to read it again (code snippet below). I thought I could reset to the beginning of the file by using the seekg( 0 ) method. This doesn’t appear to work.
Does anyone have any suggestions or comments?
// read in a file of phrases.
tfile.seekg(0); // reset data file;
while (!tfile.eof()) // loop as long as there are items left
{
tfile.getline(buffer, 49); // get an item from the file
if (!tfile.eof())
phrases.push_back(buffer);
}//end of while loop
You are calling seekg incorrectly. The absolute position (one parameter) form is only appropriate with values obtained from tellg. Instead you want to use the relative position form (two parameters). Specifically:
tfile.seekg(0,std::ios::beg);
You can also position relative to the current location (std::ios::cur) or the end (std::ios::end).