BIG PROBLEM: How to detect wrong input stream?

S

Sonny

Hi experts,

I am writing a small code to read input from a file, for example my
input file is:
X Y Z
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 Y Z
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
 
K

Karl Heinz Buchegger

Sonny said:
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";
}

...
}
 
M

Michiel Salters

[email protected] (Sonny) wrote in message news: said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top