A
Amadeus W.M.
I'm trying to read a list, and watch against failures with try/catch.
The code below reads a list of numbers from stdin. I input the numbers in
several ways:
1) a.out # then enter 1 2 3 4 5 Ctrl-D manually.
2) echo "1 2 3 4 5" | a.out
3) cat listfile | a.out
4) a.out < listfile
In each case an exception is thrown.
Is this normal behavior? Can anyone elaborate? It happens with
gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) and
gcc version 3.4.0 (Red Hat Linux 3.4.0-1)
Thanks!
#include <iostream>
#include <cstdlib>
#include <list>
using namespace std;
void print_state(ios_base::iostate & s)
{
cerr << "g: " << (s & ios_base::goodbit ? 1 : 0) << " "
<< "b: " << (s & ios_base::badbit ? 1 : 0) << " "
<< "f: " << (s & ios_base::failbit ? 1 : 0) << " "
<< "e: " << (s & ios_base::eofbit ? 1 : 0) << endl;
}
int main(int argc, char * argv[])
{
double xtmp;
list<double> x;
try{
print_state(cin.exceptions());
cin.exceptions(ios_base::badbit | ios_base::failbit);
while(cin>>xtmp) x.push_back(xtmp);
}
catch(ios_base::failure & err){
print_state(cin.exceptions());
}
return 0;
}
The code below reads a list of numbers from stdin. I input the numbers in
several ways:
1) a.out # then enter 1 2 3 4 5 Ctrl-D manually.
2) echo "1 2 3 4 5" | a.out
3) cat listfile | a.out
4) a.out < listfile
In each case an exception is thrown.
Is this normal behavior? Can anyone elaborate? It happens with
gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) and
gcc version 3.4.0 (Red Hat Linux 3.4.0-1)
Thanks!
#include <iostream>
#include <cstdlib>
#include <list>
using namespace std;
void print_state(ios_base::iostate & s)
{
cerr << "g: " << (s & ios_base::goodbit ? 1 : 0) << " "
<< "b: " << (s & ios_base::badbit ? 1 : 0) << " "
<< "f: " << (s & ios_base::failbit ? 1 : 0) << " "
<< "e: " << (s & ios_base::eofbit ? 1 : 0) << endl;
}
int main(int argc, char * argv[])
{
double xtmp;
list<double> x;
try{
print_state(cin.exceptions());
cin.exceptions(ios_base::badbit | ios_base::failbit);
while(cin>>xtmp) x.push_back(xtmp);
}
catch(ios_base::failure & err){
print_state(cin.exceptions());
}
return 0;
}