C++ file io -- eof trouble

R

Robert

I am trying to read from a input data file and I am having trouble and
ending up with another read at the eof that is garbage. I know that I
shouldn't use while(!eof) but I am pre-loading my input from the file
before even entering my while loop so that if the file is empty the
eof will work. The file that I am trying to read and parse is
generated by another parties software so I have no control over how
the file is arranged. Their setup writes directly to the file a
16byte data block that consists of 7 different variables of differing
length. The data is not delimited at all. Is there an easy way of
pulling in the 16 byte long block and ending at the end of the file
without using multiple conditional statements. Normally, when posting
to a newsgroup I include my code, but for this I have tried several
different approaches and nothing has really worked so it seems
pointless to include lots of code that hasn't functioned properly.
 
T

tom_usenet

I am trying to read from a input data file and I am having trouble and
ending up with another read at the eof that is garbage. I know that I
shouldn't use while(!eof) but I am pre-loading my input from the file
before even entering my while loop so that if the file is empty the
eof will work. The file that I am trying to read and parse is
generated by another parties software so I have no control over how
the file is arranged. Their setup writes directly to the file a
16byte data block that consists of 7 different variables of differing
length. The data is not delimited at all. Is there an easy way of
pulling in the 16 byte long block and ending at the end of the file
without using multiple conditional statements. Normally, when posting
to a newsgroup I include my code, but for this I have tried several
different approaches and nothing has really worked so it seems
pointless to include lots of code that hasn't functioned properly.

Something like:

char block[16];
while (infile.read(block, 16))
{
//process into 7 variables
}

if (!infile.eof())
{
//an error occurred.
}

should work ok.

Tom
 
T

Thomas Matthews

tom_usenet said:
I am trying to read from a input data file and I am having trouble and
ending up with another read at the eof that is garbage. I know that I
shouldn't use while(!eof) but I am pre-loading my input from the file
before even entering my while loop so that if the file is empty the
eof will work. The file that I am trying to read and parse is
generated by another parties software so I have no control over how
the file is arranged. Their setup writes directly to the file a
16byte data block that consists of 7 different variables of differing
length. The data is not delimited at all. Is there an easy way of
pulling in the 16 byte long block and ending at the end of the file
without using multiple conditional statements. Normally, when posting
to a newsgroup I include my code, but for this I have tried several
different approaches and nothing has really worked so it seems
pointless to include lots of code that hasn't functioned properly.


Something like:

char block[16];
while (infile.read(block, 16))
{
//process into 7 variables
}

if (!infile.eof())
{
//an error occurred.
}

should work ok.

Tom

The eof() member of istream does not indicate failure, but
end of the stream. There are two other functions that may
indicate failure: bad() and fail().

My preference is: !infile.good() to indicate failure or
more precisely that the stream is not good for reading
or writing.

--
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
 
R

Rob Williscroft

Thomas Matthews wrote in @newssvr33.news.prodigy.com:
tom_usenet said:
On 15 Sep 2003 08:09:34 -0700, (e-mail address removed) (Robert) wrote:
[snip]
Something like:

char block[16];
while (infile.read(block, 16))
{
//process into 7 variables
}

if (!infile.eof())
{
//an error occurred.
}

should work ok.

Tom

The eof() member of istream does not indicate failure, but
end of the stream. There are two other functions that may
indicate failure: bad() and fail().

My preference is: !infile.good() to indicate failure or
more precisely that the stream is not good for reading
or writing.

After the while loop we know that infile.fail() is true as
the loop has terminated.

So the condition to test is eof() i.e. was the failure because
an attempt was made to read *beyond* the EOF.

Rob.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

IO problem 3
"EOF"?? 7
EOF 8
Rearranging .ply file via C++ String Parsing 0
Getchar() problem 8
EOF 3
How can I view / open / render / display a pdf file with c code? 0
Bypassing the EOF marker 2

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top