473,324 Members | 2,257 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,324 software developers and data experts.

while statement

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.
Jul 19 '05 #1
4 15920
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

Jul 19 '05 #2
"John Harrison" <jo*************@hotmail.com> wrote in
news:bd************@ID-196037.news.dfncis.de:

"Thomas Matthews" <th*************@sbcglobal.net> wrote in message
news:3E**************@sbcglobal.net...
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
Jul 19 '05 #3
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
Jul 19 '05 #4


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
kb******@gascad.at
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Andrew Koenig | last post by:
PEP 315 suggests that a statement such as do: x = foo() while x != 0: bar(x) be equivalent to while True:
9
by: Ben | last post by:
I have two 'Do While Not' statements, that are getting information from the same recordset. If I comment out the first one I can get the results for the second one, and vice-versa. Why is this...
4
by: James E Koehler | last post by:
I can't get the WHILE statement to work in MySQL. The version of MySQL that I am using is: Ver 12.16 Distrib 4.0.6-gamma, for Win95/Win98 (i32) running on Windows MX. Here is the relevant...
27
by: Yan | last post by:
A lot of times when reading open software, i come across macros that are defined as follows: #define CALL_FUNCS(x) \ do { \ func1(x); \ func2(x); \ func3(x); \ } while (0);
9
by: JS | last post by:
#include <stdio.h> main(){ int c, i, nwhite, nother; int ndigit; nwhite = nother = 0; for (i = 0; i < 10; ++i)
14
by: Jan Schmidt | last post by:
Hi, in a nested do-while-loop structure I would like to "continue" the outer loop. With goto this should be no problem in while-loops. However, for do-while I cannot get it to work (without a...
11
by: Rene | last post by:
Quick question, what is the point for forcing the semicolon at the end of the while statement? See example below: x = 0; do { x = x + 1; }while (x < 3); What's the point of having the...
16
by: koutoo | last post by:
I start my code with some constants then a while statement. But I have some For statements towards the end within the While statement where I start getting some errors. I'm hoping I won't have to...
2
by: kya2 | last post by:
I am not able to create following store procedure. CREATE PROCEDURE DBSAMBA.InsertDeleteBatch(OUT norows INT ) RESULT SETS 1 LANGUAGE SQL BEGIN part1 DECLARE TOTAL_LEFT INT DEFAULT 0; ...
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
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
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.