470,602 Members | 1,694 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,602 developers. It's quick & easy.

BIG PROBLEM: How to detect wrong input stream?

Hi experts,

I am writing a small code to read input from a file, for example my
input file is:
X[i] Y[i] Z[i]
26.0 28.0 0.0
32.0 8.0 0.0

My code is:
#include<iostream>

using namespace std;
#include <fstream>
#include <iomanip>

#include <cstdlib> //exit prototype

void outputLine( double, double, double );

int main() {
//This program is trying to read the input from a certain format
double X[100];
double Y[100];
double Z[100];
//ifstream constructor opens the locations information file
ifstream inClientFile3( "Input-Location.txt", ios::in );

//Exit program if ifstream could not open file
if ( !inClientFile3 ) {
cerr << "Input file could not be opened!" << endl;
exit( 1 );
} //end if

//display each record in file
int i3in=0;
outClientFile1 << left << "Locations' Coordinates: " << endl;
inClientFile3.seekg( 30 ); //start read data stream from the second
line
while ( inClientFile3 >> x >> y >> z ){
outputLine( x, y, z );
}

return 0; // ifstream destructor closes the file

} // end main

void outputLine( double x, double y, double z ){
cout << setw( 10 ) << setprecision( 2 ) << right << x
<< setw( 10 ) << setprecision( 2 ) << right << y
<< setw( 10 ) << setprecision( 2 ) << right << z
<< endl;
};

It works fine. How ever, if i purposely change the data in the input
file (that should be of double type) to a character (meant an invalid
data type). For example I change the input file to:
X[i] Y[i] Z[i]
26.0 a 0.0
32.0 8.0 0.0

Then the program goes wrong, since it fails to read this type of data.
(It returns nothing for the whole stream)
My question is: Is there any way that can check if reading a line of
stream is successful? for example, when the program reads the "a", it
poses an error message?

Thank you very much for your commend and help.

Sonny
Jul 22 '05 #1
2 1878
Sonny wrote:

Then the program goes wrong, since it fails to read this type of data.
(It returns nothing for the whole stream)
My question is: Is there any way that can check if reading a line of
stream is successful? for example, when the program reads the "a", it
poses an error message?


Well.
In my opinion the always simplest method to deal with that, is
to *not* read int or double from the file, but instead read
an entire line as string from the file. You then use that
string to eg. do some checks and extract the parts you need
from it.

Something along the lines of:
std::string InLine;

while( getline( InLine, inClientFile3 ) )
std::stringstream Temp( InLine );

if( ! Temp >> x ) {
cout << "Error in reading x in " << InLine << "\n";
}

if( ! Temp >> y ) {
cout << "Error in reading y in " << InLine << "\n";
}

if( ! Temp >> z ) {
cout << "Error in reading z in " << InLine << "\n";
}

...
}
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
g0******@nus.edu.sg (Sonny) wrote in message news:<6c**************************@posting.google. com>...
[ SNIP ]
My question is: Is there any way that can check if reading a line of
stream is successful?


Sure. Just testing it as a bool works. Because the stream
extraction operator >> returns the stream, you can test that.

if( stream >> a >> b >> c )
{
// a,b and c were read ok
}

With rdstate() you can figure out why, with clear() you
can continue.
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Matt Chaplain | last post: by
7 posts views Thread by David Tandberg-Johansen | last post: by
7 posts views Thread by Thomas Sourmail | last post: by
7 posts views Thread by Kay | last post: by
11 posts views Thread by Markus | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.