fstream::clear

Hello guys,
I got the eofbit and failbit when we readed the file end.
My question is if How can I clear the eof and fail stat whith out fstream::clear.
Is the close doesn't work?
Why do you want to change the state without using clear?
see my code as below:
ifstream in("sample");
if (in.fail())
error(); //open successful
int i;
while (in >> i) ; //I got the eof and fail after while
in.close();
in.clear();// I closed the file why I need clear

in.open("sample");
if (in.fail())
error(); //if I don't use clear then open failed.
Is the close doesn't work in C++ we should use clear too?WHY C used fclose(in) only and works fine?
Maybe it fails when you try to close it. I tried an don't have to call clear after close.
Which compiler / version / OS are you using?
Hi Mattia,
Thank you.
Please see the full code:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{int aver,bzc,m,sum=0;
int count=0;
ifstream fin;

fin.open("c:\\sample.txt");
if (fin.fail())
{ cout<<"Input file opening failed.\n";
exit(1);
}
while(fin>>m)//!fin.eof()
{
if(m!=' ')
{sum=sum+m;
count++;
}
}
fin.close();
//fin.clear(fin.rdstate() ^ (fstream::eofbit |fstream::failbit) );
cout <<fin.fail()<<endl;cin.get();
aver=sum/count;

cout<<"the average is:"<<aver<<endl;
fin.open("c:\\sample.txt");
if (fin.fail())
{ cout<<"Input file opening failed.\n";cin.get();
exit(1);
}
while(fin>>m)
{if(m!=' ')
{bzc=(m-aver)*(m-aver);
cout<<"the bzc is"<<bzc<<endl;
}
}
fin.close();
cin.get();

return 0;
}
Please use [code][/code] tags.
Do you get fail after the second open?
Yes,that is my problem.
I tried your code and works for me. Which compiler are you using?
I'm using DevCpp 4.9.9.2 and work in Windows XP sp3
Dev-C++ is old, change IDE
Okey,anywhere,Thanks!
Topic archived. No new replies allowed.