txt file input isnt working correctly..
Apr 23, 2011 at 12:45am UTC
everything works fine except that it won't stop when it reaches the end of the file, and keeps reading the last word over and over.
here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
class Note
{
public :
void output()
{
char mynote[256];
cout << "Enter your note: " ;
// output to file
cin.getline (mynote,256);
ofstream outmonday("monday.txt" );
outmonday << mynote;
//
}
void input()
{
char b[256];
ifstream inmonday("monday.txt" );
while (!ifstream("monday.txt" ).eof())
{
inmonday >> b;
cout <<b << " " ;
if (ifstream("monday.txt" ).fail()) break ;
}
}
};
int main ()
{
Note note;
note.output();
note.input();
system("pause" );
return 0;
}
note.output() works fine, the trouble is with note.input()
everything works fine except that it won't stop when it reaches the end of the file, and keeps reading the last word over and over.
can someone please tell me what i am doing wrong?
i think it has something to do with "while (!ifstream("monday.txt").eof())"
but the synax is acceptable.. so i am lost... please help!
Last edited on Apr 23, 2011 at 12:50am UTC
Apr 23, 2011 at 12:51am UTC
The condition of the while loop is incorrect. It should be inmonday.eof().
Same goes for the if inside the while. Should be inmonday.fail().
Last edited on Apr 23, 2011 at 12:51am UTC
Apr 23, 2011 at 12:58am UTC
wow.. that was simple.. thanks! it works now =)
Topic archived. No new replies allowed.