Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > BIG PROBLEM: How to detect wrong input stream?

Reply
Thread Tools

BIG PROBLEM: How to detect wrong input stream?

 
 
Sonny
Guest
Posts: n/a
 
      05-06-2004
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
 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      05-06-2004
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Michiel Salters
Guest
Posts: n/a
 
      05-07-2004
(E-Mail Removed) (Sonny) wrote in message news:<(E-Mail Removed). 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
GIDS 2009 .Net:: Save Big, Win Big, Learn Big: Act Before Dec 29 2008 Shaguf ASP .Net 0 12-26-2008 09:29 AM
GIDS 2009 .Net:: Save Big, Win Big, Learn Big: Act Before Dec 29 2008 Shaguf ASP .Net Web Controls 0 12-26-2008 06:11 AM
GIDS 2009 Java:: Save Big, Win Big, Learn Big: Act Before Dec 29 2008 Shaguf Python 0 12-24-2008 07:35 AM
GIDS 2009 Java:: Save Big, Win Big, Learn Big: Act Before Dec 29 2008 Shaguf Ruby 0 12-24-2008 05:07 AM



Advertisments