By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,388 Members | 1,780 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

I/O exceptions

P: n/a
I'm trying to read a vector from a file, while checking the correctness of
the input. I do this by setting the failbit.

double tmp;
vector<double> x;
fstream IN(argv[1], ios::in);

IN.exceptions(std::ios_base::failbit);
try{
while(IN>>tmp)
x.push_back(tmp);
}
catch(...){

}

But that's not right, because when IN reads the EOF an exception is raised.
I only want to catch the potential input errors other than the EOF.
What's the right way to do this?

Thanks.
Dec 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Amadeus W. M." <am*******@cablespeed.com> wrote in message
news:pa****************************@cablespeed.com ...
I'm trying to read a vector from a file, while checking the correctness of
the input. I do this by setting the failbit.

double tmp;
vector<double> x;
fstream IN(argv[1], ios::in);

IN.exceptions(std::ios_base::failbit);
try{
while(IN>>tmp)
x.push_back(tmp);
}
catch(...){

}

But that's not right, because when IN reads the EOF an exception is
raised.
That's because in this case reaching end of file sets not only eofbit, but
failbit as well (the last conversion failed).
I only want to catch the potential input errors other than the EOF.
What's the right way to do this?


IN.exceptions(0);

while(IN >> tmp)
x.push_back(tmp);

if(!IN.eof())
std::cerr << "Error\n";

-Mike
Dec 22 '05 #2

P: n/a
Amadeus W. M. wrote:
I'm trying to read a vector from a file, while checking the correctness of
the input. I do this by setting the failbit.

double tmp;
vector<double> x;
fstream IN(argv[1], ios::in);

IN.exceptions(std::ios_base::failbit);
try{
while(IN>>tmp)
x.push_back(tmp);
}
catch(...){

}

But that's not right, because when IN reads the EOF an exception is raised.
I only want to catch the potential input errors other than the EOF.
What's the right way to do this?

Thanks.


IIRC, the failbit indicates that the *next* extraction operation on the
ifstream will fail, and if the EOF is found, the next extraction must
fail. Perhaps a more standard way to do it would be:

ifstream in( "somefile.txt" );
vector<double> v;

double d;
while( in >> d )
v.push_back( d );
//or
//
//copy( istream_iterator<double>( in ),
// istream_iterator<double>(),
// back_inserter( v ) );

if( in.bad() || !in.eof() )
{
// Some error occurred
}

If you really want to use exceptions, you catch block could look
something like:

catch( const exception& e )
{
if( in.bad() || !in.eof() )
{
// Some error occurred
}
}

Cheers! --M

Dec 23 '05 #3

P: n/a

mlimber wrote:
Amadeus W. M. wrote:
I'm trying to read a vector from a file, while checking the correctness of
the input. I do this by setting the failbit.

double tmp;
vector<double> x;
fstream IN(argv[1], ios::in);

IN.exceptions(std::ios_base::failbit);
try{
while(IN>>tmp)
x.push_back(tmp);
}
catch(...){

}

But that's not right, because when IN reads the EOF an exception is raised.
I only want to catch the potential input errors other than the EOF.
What's the right way to do this?

Thanks.


IIRC, the failbit indicates that the *next* extraction operation on the
ifstream will fail, and if the EOF is found, the next extraction must
fail. Perhaps a more standard way to do it would be:

ifstream in( "somefile.txt" );
vector<double> v;

double d;
while( in >> d )
v.push_back( d );
//or
//
//copy( istream_iterator<double>( in ),
// istream_iterator<double>(),
// back_inserter( v ) );

if( in.bad() || !in.eof() )
{
// Some error occurred
}


I think calling bad() here is redundant. You have got to this point
because reading stopped. If all you care about is whether some error
occurred then all you need to check is eof because if no error occurred
the only other reason reading would have stopped is eof.

Gavin Deane

Dec 23 '05 #4

P: n/a
On Thu, 22 Dec 2005 15:32:09 -0500, Amadeus W. M. wrote:
I'm trying to read a vector from a file, while checking the correctness of
the input. I do this by setting the failbit.

double tmp;
vector<double> x;
fstream IN(argv[1], ios::in);

IN.exceptions(std::ios_base::failbit);
try{
while(IN>>tmp)
x.push_back(tmp);
}
catch(...){

}

But that's not right, because when IN reads the EOF an exception is raised.
I only want to catch the potential input errors other than the EOF.
What's the right way to do this?

Thanks.


Thank you all for the suggestions, I don't know what I was thinking.
This is what I had in mind, but I could not spell out until after reading
this thread:

template <class T>
istream & operator>>(istream & IN, vector<T> & x)
{
T tmp;
while(IN>>tmp)
x.push_back(tmp);

if(!IN.eof())
throw string("Bad input.");

return IN;
}

then in main(), a simple

try{
cin >> x;
}
catch(string & err){
cerr << err << endl;
// handle the error
}

Dec 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.