boost::filesystem::ifstream and .eof()

Dear Boost users --I have a simple question about how to test for eof. I usually use: std::ifstream inputDataStream("path to file"); and test for EOF by: inputDataStream.eof() I tried the same thing using: boost::filesystem::basic_ifstream<char> inputDataStream("path to file", std::ios_base::in); and test for EOF by: inputDataStream.eof() but its not working. Am I using the .eof() correctly in the boost version of basic_ifstream? Here is the broken code using the boost version of basic_ifstream: int main() { boost::filesystem::path dataPath("/tao/data"); if ( !exists(dataPath) ) { std::cerr << "Path does not exist!"; return EXIT_FAILURE; }; for (boost::filesystem::directory_iterator fileIterator(dataPath); fileIterator != boost::filesystem::directory_iterator(); fileIterator++) { const boost::filesystem::path InputFileName = dataPath/fileIterator -> leaf(); boost::filesystem::basic_ifstream<char> inputDataStream(InputFileName, std::ios_base::in); int i = 1; int previousIterator = 0; trialOnsetWrite(); while ( !inputDataStream.eof() ) { if ( (previousIterator + 3) == i) { immobileWrite(i); previousIterator = previousIterator + 3; } else { dataStructureWrite(i); }; i++; }; trialOffsetWrite(i); }; return (0); }; Very appreciative, Patrick

Hi! pktao@uwm.edu schrieb:
while ( !inputDataStream.eof() ) {
if ( (previousIterator + 3) == i) {
immobileWrite(i);
previousIterator = previousIterator + 3;
}
else {
dataStructureWrite(i);
};
i++;
};
I cannot see where data is read. Anyway: - first: Test for any error: "while(inputDataStream) {...}" - second: Test after read: int sum = 0, i; while(inputDataStream >> i) { sum +=i; } When using istream_iterator<T>, then just test against an end interator: std::for_each(istream_iterator<int>(inputDataStream), istream_iterator<int>(), &foo); All of the above is true for both boost::filesystem::basic_ifstream and std::ifstream. It is true for any std::istream. HTH, Frank
participants (2)
-
Frank Birbacher
-
pktao@uwm.edu