In my assignment i have a problem with reading a file. See the following code segment.
std::string data;
std::ifstream fileRead;
fileRead.open("a.txt");
while (fileRead >> data)
{
long a = fileRead.tellg();
fileRead.seekg (a+1, ios::beg);
std::string check;
//some code here
while (fileRead >> check)
{
//some code here
}
fileRead.seekg (a+1, ios::beg);
}
I have to check how many same words are in the file. My logic is, i read a word and keep it in data. Then i continue reading after that word using fileRead.seekg (a+1, ios::beg); Then i compare each of the words in the file.
After checking whole file i again put my fileobject to next word using this line fileRead.seekg (a+1, ios::beg); But this line is not working. I mean first while loop just work once. Someone please help me out here.
By the way, i am totally new in file operation. So my logic or concept may not be correct in that case tell me what is the right way to do this? But i need to know why first while loop ends just after reading one word?
When you read file as
while(fileRead >> data), the a failure flag is set for the stream object, before exiting the loop. In fact, it is set that is why it exits the loop. You need to clear this failure flagas: