Saturday, May 22, 2010

Using fstream (Visual C++)?

//File C:\1.txt contains "123456"


ifstream in ("c:\\1.txt", ios_base::in);


char c;


//reading while next char won't be eof


while(in.peek()!=char_traits%26lt;char%26gt;::eo...


in %26gt;%26gt; c;


//after that c = 6


//now moving position to begin of file


in.seekg(0, ios_base::beg);


//and starting read again


for (int i=0;i%26lt;6;i++)


in %26gt;%26gt; c;





the problem is after in.seekg(0, ios_base::beg);


curent position remains on '6'. What's the problem,


can anyone help,

Using fstream (Visual C++)?
When you enter the second input loop, the fail bit is still set from when you hit EOF the first time. Because the fail bit is set, nothing is read and c still contains 6. After falling out of the while loop, call in.clear() to clear the fail bit. Then it will work.


No comments:

Post a Comment