473,387 Members | 1,464 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,387 software developers and data experts.

C++ file io -- eof trouble

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.
Jul 19 '05 #1
3 8240
On 15 Sep 2003 08:09:34 -0700, rm******@ncsu.edu (Robert) wrote:
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
Jul 19 '05 #2
tom_usenet wrote:
On 15 Sep 2003 08:09:34 -0700, rm******@ncsu.edu (Robert) wrote:

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

Jul 19 '05 #3
Thomas Matthews wrote in news:2Sk9b.2315$ZM5.769
@newssvr33.news.prodigy.com:
tom_usenet wrote:
On 15 Sep 2003 08:09:34 -0700, rm******@ncsu.edu (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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #4

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

Similar topics

6
by: o'seally | last post by:
solaris/linux admins/rookie_developers that battle with this error are probably all frustrated when it happens. i bet you're also somehow frustrated by this seemingly unsolvable error :-) ...take...
0
by: Masahiro Ito | last post by:
While develop the app, some forms etc. can be used right away, and are changing quickly. If I develop the app on file share, is it no problem for users to be running app from their mapped file...
5
by: Scott Tilton | last post by:
I am having a terrible time getting this to work. I am hoping someone out there can help me with very specific code examples. I am trying to get the linked tables in my Access 97 database to be...
6
by: Tom | last post by:
I am having trouble when I read a file and another process is trying to update it. So I need a rountine to copy a file in asp.net. Can anyone assist? Thanks Tom
0
grassh0pp3r
by: grassh0pp3r | last post by:
Hello, I'm trying to make a very simple comments page on my site using PHP and am having problems somewhere. I am very new to PHP. I was able to create one that works with comments appended, but...
4
by: Sanchit | last post by:
I want to know thta how can i edit a file in C++ For Example my file is Mr XyZ FFFFFF 65 And now i want go change this number 65 to 87.... how can i Do this..... I...
5
by: Ravi | last post by:
How can I prevent a header file from being included twice?
3
by: d3vkit | last post by:
I've been using apache and php to create clean urls for a while, and suddenly the other day it just stopped working. My host is godaddy.com and I think this might be the source of my trouble, but...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
2
blazedaces
by: blazedaces | last post by:
I'm designing a GUI for autonomous robot interaction, but that's not so important. To figure out the ins and outs of a certain aspect of PyQt I usually just do what I did when I learning Qt, I grab...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.