473,513 Members | 2,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I/O exceptions

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
4 1548

"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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
5266
by: David Turner | last post by:
Hi all I noticed something interesting while testing some RAII concepts ported from C++ in Python. I haven't managed to find any information about it on the web, hence this post. The problem...
21
2196
by: dkcpub | last post by:
I'm very new to Python, but I couldn't find anything in the docs or faq about this. And I fished around in the IDLE menus but didn't see anything. Is there a tool that can determine all the...
26
2857
by: OvErboRed | last post by:
I just read a whole bunch of threads on microsoft.public.dotnet.* regarding checked exceptions (the longest-running of which seems to be <cJQQ9.4419 $j94.834878@news02.tsnz.net>. My personal...
9
2321
by: Gianni Mariani | last post by:
I'm involved in a new project and a new member on the team has voiced a strong opinion that we should utilize exceptions. The other members on the team indicate that they have either been burned...
6
2817
by: RepStat | last post by:
I've read that it is best not to use exceptions willy-nilly for stupid purposes as they can be a major performance hit if they are thrown. But is it a performance hit to use a try..catch..finally...
14
3452
by: dcassar | last post by:
I have had a lively discussion with some coworkers and decided to get some general feedback on an issue that I could find very little guidance on. Why is it considered bad practice to define a...
8
2235
by: cat | last post by:
I had a long and heated discussion with other developers on my team on when it makes sense to throw an exception and when to use an alternate solution. The .NET documentation recommends that an...
1
2369
by: Anonieko | last post by:
Understanding and Using Exceptions (this is a really long post...only read it if you (a) don't know what try/catch is OR (b) actually write catch(Exception ex) or catch{ }) The first thing I...
2
2953
by: Zytan | last post by:
I know that WebRequest.GetResponse can throw WebException from internet tutorials. However in the MSDN docs: http://msdn2.microsoft.com/en-us/library/system.net.webrequest.getresponse.aspx It...
0
6487
RedSon
by: RedSon | last post by:
Chapter 3: What are the most common Exceptions and what do they mean? As we saw in the last chapter, there isn't only the standard Exception, but you also get special exceptions like...
0
7158
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7380
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
5085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.