while statement

Discussion in 'C++' started by muser, Jun 23, 2003.

  1. muser

    muser Guest

    I have a logical error in my program, I have submitted the program and
    my tutor hasn't listed the other problems with the code, but said that
    the program won't run because of a while statement.
    The while statement is as follows.

    while(infile.peek() != EOF)
    {
    infile.getline(temp1, max);

    };

    if statement

    if statement

    if statement

    etc.

    The code compilers ok, but the program won't run because (tutor's
    explaination)
    it is going to the end of the file and reading nothing.
    I've tried enclosing all other statements within the while statement,
    but that appears not to work. Like so:

    while(infile.peek() != EOF)
    {
    infile.getline(temp1, max);



    if statement

    if statement

    if statement

    etc.

    };

    If anyone could help I would be extremely grateful.
    muser, Jun 23, 2003
    #1
    1. Advertising

  2. muser wrote:
    > I have a logical error in my program, I have submitted the program and
    > my tutor hasn't listed the other problems with the code, but said that
    > the program won't run because of a while statement.
    > The while statement is as follows.
    >
    > while(infile.peek() != EOF)
    > {
    > infile.getline(temp1, max);
    >
    > };
    >

    [snip]
    >If anyone could help I would be extremely grateful.


    The peek() method of istream may not return an EOF value. The peek()
    method returns the next character in the stream and fails if there
    isn't one or the stream failed while trying. Some filesystems don't
    have a character that represents EOF. Some MSDOS systems used the
    value of 0x1a for an EOF marker, while others used 0x04 (EOT).

    Use the good(), fail(), bad() methods for checking if a
    stream has reached EOF.

    Read this section of the FAQ:
    http://www.parashift.com/c -faq-lite/input-output.html

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Jun 23, 2003
    #2
    1. Advertising

  3. muser

    Ben Payne Guest

    "John Harrison" <> wrote in
    news:bd8nfb$qeadq$:

    >
    > "Thomas Matthews" <> wrote in message
    > news:...
    >> muser wrote:
    >> > I have a logical error in my program, I have submitted the program
    >> > and my tutor hasn't listed the other problems with the code, but
    >> > said that the program won't run because of a while statement.
    >> > The while statement is as follows.
    >> >
    >> > while(infile.peek() != EOF)
    >> > {
    >> > infile.getline(temp1, max);
    >> >
    >> > };
    >> >

    >> [snip]
    >> >If anyone could help I would be extremely grateful.

    >>
    >> The peek() method of istream may not return an EOF value.

    >
    > peek is defined to return EOF on end of file (assuming infile is
    > derived from istream, or iostream).
    >
    > I see no logical error with the OP's code, but Jon's suggestion is
    > undoubtedly cleaner.
    >
    > john
    >
    >
    >


    I think the most concise, and foolproof way to do this is just:

    while(!infile.getline(temp1, max).eof());

    this is valid since getline returns a reference to the stream.
    the only downside is addition of a function call, but that's
    what it's there for.


    ben
    Ben Payne, Jun 27, 2003
    #3
  4. muser

    Jim Fischer Guest

    Ben Payne wrote:
    > I think the most concise, and foolproof way to do this is just:
    >
    > while(!infile.getline(temp1, max).eof());
    >
    > this is valid since getline returns a reference to the stream.
    > the only downside is addition of a function call, but that's
    > what it's there for.


    ARGH! THIS WILL NOT WORK! (Sorry for the shouting, but...)

    Note that any given getline() call can successfully read data from the
    input stream into 'test1' and, in the process of doing so, detect the
    end-of-file condition (and consequently set the stream's eofbit state
    flag). IOW, a single getline() call can both read data into 'test1'
    *AND* set the stream's eofbit state flag. So your while(!eof) loop
    introduces an "off by one" error for this particular case -- i.e., the
    program reads some data from infile into test1, it detects/reports the
    EOF condition, and the while() loop exits. Notice that the last bit of
    file data in 'test1' IS NOT PROCESSED BY THE PROGRAM -- i.e., the data
    in test1 is not processed by the code in the body of the while() loop --
    because the EOF condition caused the program to break out of the while()
    loop too soon! Whoops...

    FWIW, this is one of two different "off by one" logic errors that *will
    occur* when you write a loop construct like this,

    while ( ! end-of-file on an input stream ) ...

    A "more better" loop test would be this:

    while ( data is successfully read from infile into test1 ) {
    process the data in test1;
    }

    e.g.,

    while ( infile.getline(test1,max).gcount() ) ...

    [n.b. The gcount() method returns the number of characters extracted
    from an input stream object by the last unformatted input member
    function(*) called on the object.

    (*) e.g., the get() and getline() member functions perform unformatted input
    ]


    FWIW2, there is yet another "gotcha" here. Note that if the getline()
    call completely fills the buffer 'test1' without also reading in the
    delimiter character (i.e., the buffer 'test1' is too small to hold the
    entire line of input), then getline() will assert the input stream's
    'failbit' flag to signal this "buffer full" condition. So the program
    must also check for, and respond to, this failbit condition -- e.g.,

    while ( infile.getline(test1,max).gcount() ) {
    // Clear the stream's 'failbit' state flag
    infile.clear( infile.rdstate() & ~ios::failbit );
    ...
    }

    --
    Jim

    To reply by email, remove "link" and change "now.here" to "yahoo"
    jfischer_link5809{at}now.here.com
    Jim Fischer, Jun 27, 2003
    #4
  5. Ben Payne wrote:
    >
    >
    > I think the most concise, and foolproof way to do this is just:
    >
    > while(!infile.getline(temp1, max).eof());
    >
    > this is valid since getline returns a reference to the stream.
    > the only downside is addition of a function call, but that's
    > what it's there for.


    In addition what Jim alredy had to say.

    .... it is foolproof until you try to read from a file
    from a floppy disk which has a demaged sector in the middle
    of your file.
    The getline() will fail, but not because of eof


    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Jun 27, 2003
    #5
    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:
    3
    Views:
    799
  2. Jay McGavren
    Replies:
    11
    Views:
    1,127
    Alan Krueger
    Jan 16, 2006
  3. tedsuzman
    Replies:
    2
    Views:
    7,074
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  4. Ted
    Replies:
    1
    Views:
    460
    Duncan Booth
    Jul 22, 2004
  5. Replies:
    21
    Views:
    1,050
    Giannis Papadopoulos
    Aug 2, 2005
Loading...

Share This Page