-
seekg woos
Trying to do some random file access. But seekg() or get() is not working as I expected... Of course what I was expecting is the problem here :D
Code:
//.... declarations and stuff
pifile.open("teste.txt", ios::in | ios::nocreate);
if(!pfile) exit(1);
cout << "Displaying the file:" << endl;
while(pifile.get(inchar)) cout << inchar;
cout << endl << "Returning to the begin:" << endl;
pifile.seekg(0L, SEEK_SET);
cout << "Reading first line only" << endl;
for(int i=1; i<=7; i++)
{
if(pifile.get(inchar)) cout << inchar;
else break;
}
//....
The first while block displays the file nicely. But although I get no compile errors, after I seekg it to the beginning, the for loop that follows doesn't display anything. In fact while tracing, not even the break statement is processed. After testing if(pifile.get(inchar)), it immediatly goes to the end bracket.
What am I doing wrong?
-
You need to clear the EOF flag after reading to the end of the file the first time.
I think it's this:
>pifile.clear();
-
It worked.
Thanks a bunch Hammer :)
-
Help!!
hi, i need, help, i want to now, how i can use the direct files, like fseek, etc, in a program , thanks