ifstream --- error maybe?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jason

    #1

    ifstream --- error maybe?

    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
  • Niels Dybdahl

    #2
    Re: ifstream --- error maybe?

    > I wrote a piece of code that reads read in about 1000 int values from[color=blue]
    > 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.[/color]

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

    Niels Dybdahl


    Comment

    • Jacek Dziedzic

      #3
      Re: ifstream --- error maybe?

      Jason wrote:
      [color=blue]
      > 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.[/color]

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

      - J.

      Comment

      • Tom Widmer

        #4
        Re: ifstream --- error maybe?

        On 27 Oct 2004 20:36:58 -0700, jspalding@gmail .com (Jason) wrote:
        [color=blue]
        >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.[/color]

        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

        Comment

        Working...