BIG PROBLEM: How to detect wrong input stream?

Discussion in 'C++' started by Sonny, May 6, 2004.

  1. Sonny

    Sonny Guest

    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
     
    Sonny, May 6, 2004
    #1
    1. Advertising

  2. 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
     
    Karl Heinz Buchegger, May 6, 2004
    #2
    1. Advertising

  3. (Sonny) wrote in message news:<>...
    [ 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.
     
    Michiel Salters, May 7, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    9
    Views:
    652
    Alex Buell
    Apr 27, 2006
  2. Shaguf
    Replies:
    0
    Views:
    513
    Shaguf
    Dec 24, 2008
  3. Shaguf
    Replies:
    0
    Views:
    479
    Shaguf
    Dec 26, 2008
  4. Shaguf
    Replies:
    0
    Views:
    257
    Shaguf
    Dec 26, 2008
  5. Shaguf
    Replies:
    0
    Views:
    236
    Shaguf
    Dec 24, 2008
Loading...

Share This Page