473,325 Members | 2,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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

Similar topics

4
by: Matt Chaplain | last post by:
Hi there. I'm writing a program that uses the Telnet protocol over TCP/IP sockets. Of course, that has no bearing here, so I'll rephrase that in Standard C++ :) In essense, I'm trying to...
8
by: dbuser | last post by:
Hi, I need help on a problem, as described below. I am reading a file "input.txt"which has data like this: abc def gh izk lmnopq rst uvwxyz I am using fstream object to read the file and...
7
by: David Tandberg-Johansen | last post by:
Hello! I am an newbie, and I am a litle stuck here. Could anyone please tell me what I am doing wrong? Here is my code: #include <stdio> #include <stdlib> struct node {
7
by: Thomas Sourmail | last post by:
Hi, I hope I am missing something simple, but.. here is my problem: I need my program to check the last column of a file, as in : a b c d target ref 0 0 0 0 1 a 1 0 0 0 1.5 b 2 0 0 0 2 c
7
by: Kay | last post by:
1) If i want to read data from a txt file, eg John; 23; a Mary; 16; i How can I read the above data stopping reading b4 each semi-colon and save it in three different variables ? 2) If I...
18
by: Radith | last post by:
HI all; I have created a program just to get an understanding of strcpy. (still an amateur developer). I use scanf("%s", &first) in order to get the input to the variable first (which is char...
5
by: Barry Mossman | last post by:
Hi, can I detect whether my class is running within the context of a Console application, vs say a WinForm's application ? also does anyone know whether the compiler or runtime is smart enough...
11
by: Markus | last post by:
Hi, I want to get an integer from the user. If the user inserts a character instead of an int the program goes crazy. So I tried something like this: This program works pretty well. But if...
4
by: seedstorm | last post by:
I am using a HtmlInputFile control in ASP.NET 2.0 to upload a file in a UserControl. After upload, I am examining the HttpPostedFile property of this object to read the bytes of the uploaded file's...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.