Dec 12, 2010 at 12:07am UTC
hi, I have this code and it reads the file, but it keeps printing the last character of the file forever, how can I make it stop?
ifstream file;
file.open("filename.txt", ios::in);
file >> c;
while (c != EOF)
{
cout << c;
file >> c;
}
Dec 12, 2010 at 1:44am UTC
Unlike fgetc, which will give you EOF on EOF, cin will become a zombie object and not respond toyour calls. This is one downside of cin/cout. You need to check whether cin.eof() returns true, instead of checking whether it gives EOF.
Dec 12, 2010 at 3:38am UTC
hmmm i'm new in programming and didn't understand really well what you said but did this and got the same results as before u__u
ifstream file;
file.open("filename.txt", ios::in);
file >> c;
while (c != cin.eof())
{
cout << c;
file >> c;
}
NOTE: im using Visual c++ 2008 express ed (if it helps)
Dec 12, 2010 at 4:10am UTC
ok, problem solved, thanks
ifstream file;
file.open("filename.txt", ios::in);
file >> c;
while (!file.eof())
{
cout << c;
file >> c;
}