1) What's the difference between:
myfile.peek() != EOF and !myfile.eof() if I declare ifstream myfile("test.txt")?
2) test.txt
Code:
12 05 2005 dad da ada
12 01 2001 d d ddadda
04 11 2005 s s s
12 05 2005 dad da ada
test.cpp
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string a, b, c, d;
ifstream myfile;
myfile.open("test.txt");
if (myfile.is_open())
{
while (myfile.peek() != EOF)
{
getline(myfile, a, ' ');
getline(myfile, b, ' ');
getline(myfile, c, ' ');
getline(myfile, d, '\n');
cout << a << ' ' << b << ' ' << c << ' ' << d << endl;
}
myfile.close();
}
}
If "myfile.peek() != EOF" is used, the output is:
Code:
12 05 2005 dad da ada
12 01 2001 d d ddadda
04 11 2005 s s s
12 05 2005 dad da ada
However If "!myfile.eof()" is used, the output has an extra line with first word truncated:
Code:
12 05 2005 dad da ada
12 01 2001 d d ddadda
04 11 2005 s s s
12 05 2005 dad da ada
05 2005 dad da ada
Anybody knows why?