ifstream --- error maybe?

Discussion in 'C++' started by Jason, Oct 28, 2004.

  1. Jason

    Jason Guest

    I wrote a piece of code that reads read in about 1000 int values from
    a text file. I put a cout statement to verify that the input in the
    code was working as anticipated. It seems to work fine until some
    point when it just ouputs 0's. So I then took the code and made a app
    that just read the file and it worked fine.

    Does anyone have a suggestion.

    Jason
     
    Jason, Oct 28, 2004
    #1
    1. Advertising

  2. > I wrote a piece of code that reads read in about 1000 int values from
    > a text file. I put a cout statement to verify that the input in the
    > code was working as anticipated. It seems to work fine until some
    > point when it just ouputs 0's. So I then took the code and made a app
    > that just read the file and it worked fine.
    >
    > Does anyone have a suggestion.


    Reduce the code to the smallest amount that still produces the problem and
    post that code.

    Niels Dybdahl
     
    Niels Dybdahl, Oct 28, 2004
    #2
    1. Advertising

  3. Jason wrote:

    > I wrote a piece of code that reads read in about 1000 int values from
    > a text file. I put a cout statement to verify that the input in the
    > code was working as anticipated. It seems to work fine until some
    > point when it just ouputs 0's. So I then took the code and made a app
    > that just read the file and it worked fine.
    >
    > Does anyone have a suggestion.


    Yes. Post the code. We are programmers, not clairvoyants.

    - J.
     
    Jacek Dziedzic, Oct 28, 2004
    #3
  4. Jason

    Tom Widmer Guest

    On 27 Oct 2004 20:36:58 -0700, (Jason) wrote:

    >I wrote a piece of code that reads read in about 1000 int values from
    >a text file. I put a cout statement to verify that the input in the
    >code was working as anticipated. It seems to work fine until some
    >point when it just ouputs 0's. So I then took the code and made a app
    >that just read the file and it worked fine.
    >
    >Does anyone have a suggestion.


    How are you checking for the end of the input? You should do something
    like this:

    int i = 0;
    for(; i != 1000; ++i)
    {
    int val;
    if (!(stream >> val))
    break;
    //assign val to whatever
    }

    if (i < 1000)
    {
    //something went wrong
    //check stream.eof(), stream.bad() and stream.fail().
    }

    Tom
     
    Tom Widmer, Oct 28, 2004
    #4
    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. Giulio
    Replies:
    2
    Views:
    1,867
    Jon Bell
    Jun 24, 2003
  2. Guest
    Replies:
    5
    Views:
    645
  3. Lingyun Yang
    Replies:
    2
    Views:
    984
    David Harmon
    Apr 11, 2004
  4. Ville Vainio
    Replies:
    11
    Views:
    639
    Hamish Lawson
    Aug 10, 2004
  5. Massimo Soricetti

    (help) Runtime error in ifstream/ios class

    Massimo Soricetti, Jan 13, 2006, in forum: C++
    Replies:
    2
    Views:
    453
    Massimo Soricetti
    Jan 14, 2006
Loading...

Share This Page