In the following code, the second loop does not work once in Xcode, but in Visual Studio it works as it should:

int temp; char symbol; std::ifstream ifs("a.txt"); while (ifs>>temp) cout<<temp<<' '; // перебор последовательности, как чисел. cout<<endl; ifs.seekg(0,ios::beg); // установка курсора в начало файла. ifs.clear(); // очищение флага состояния. while (ifs.get(symbol)) // перебор последовательности, как символов. if (symbol!=' ' && symbol!='\n') cout<<symbol<<' '; ifs.close(); 

The second cycle does not go through even once when the end of the file is reached in the first cycle. All tests performed indicate a problem with the state flags. But the state flags after the 'clear ()' method are cleared, as it should be. Therefore, I can not deal with this problem. Unsubscribe, please, those who faced this problem?

  • Try swapping seekg and clear - yrHeTaTeJlb

1 answer 1

The difference is that the std::istream::seekg in c++11 :

The function clears the eofbit flag, if set before the call.

The function clears the eof flag if it was set before the call.

and in c++98 :

If the flag is set before the call, it sets the function fails and returns.

If the eof flag was set before the function is called with an error ( failbit set).

Those. if to collect with support of c++11 , then seekg normally work if already were at the end of the file. Otherwise, it will return an error. Accordingly, it is enough to swap the calls for seekg and clear so that it works in both cases.

  • Thank. Did not fix attention when reading the documentation on this point, my mistake) - Arseniy Kapran