Printing the last item of a structure twice

R

Richard Bos

Keith Thompson said:
The above was written by RjY <[email protected]>.

No, it wasn't; it was written by me.
Rafael, please don't delete attribution lines for quoted text.

And please don't top-post. See the following for more information:
http://www.caliburn.nl/topposting.html
http://www.cpax.org.uk/prg/writings/topposting.php
Quite.
Second, you've changed the meaning of the code. This:
fread(...)
is true if fread returns any non-zero value, whereas this:
fread(...) == 1
is true only if fread returns 1. As it happens, fread with the value
1 as its third argument can only return 1, so it doesn't matter in
this case. But consider what would happen if, rather than fread, you
were using fscanf, which can return either the number of items scanned
(possibly 0) or EOF.

More to the point, consider what would happen if you changed the 1 to an
n, because your requirements change and your code must now be able to
read a number of elements at once. You now want to know whether it read
all the elements, not (or rarely, anyway) whether it read any at all
regardless how many.

Richard
 
R

Richard Bos

Rafael Anschau said:
Is it a default procedure here to have a fake profile and using it
when all else fails ? I thought about having one but it=B4s such a
hassle.

Last fucking warning, clown. Make it easy for others to help you - don't
be an asshole to the very people whose assistance you sought in the
first place.

Richard
 
R

Rafael Anschau

Last fucking warning, clown.

Or else ? Please killfile me Mr Richard, the fucked up madman. Do I
disturb you ? If yes, seek
psychological treatment together with the other ones.
Make it easy for others to help you - don't
be an asshole to the very people whose assistance you sought in the
first place.

I respond badly to people who answer humble questions with arrogance.
If that´s your case, pick a ticket and go to the end of the line. I don
´t have this psychotic American concern over my self image and what
other´s think of me. And please take your medication.

Rafael
 
L

lawrence.jones

jameskuyper said:
I assume that you're just making a joke, but actually, it's been
argued that this is precisely what the ISO's rules require. According
to the person who argued this point (who knew a great deal more than I
do about ISO rules), when C90 ceased to be the official standard,
having been replaced by the approval of C99, all other references to
the C standard in other standards are automatically considered to
refer to the new standard - even if those references explicitly
specify a particular, older, version of the C standard (as is the case
with the reference to it in the C++ standard)!

No. The rules are quite clear that any reference that includes a date
refers only to that specific version. Only undated references (which
are rarely used) automatically refer to the latest version. At one time
the standard introductory text suggested that people might want to
investigate the applicability of more recent versions and reach private
agreements to do so, but that text no longer appears in the current
drafting rules.
 
C

chad

Hoping is nice. But hoping that you have enough money to support your
family doesn't actually give you any money, and hoping that your C++
problem isn't off topic in a C newsgroup doesn't remove any of the
numerous uses of C++ features from your program. Your actual problem
is with the part of the C standard library that is inherited by C++
from C, and is exactly the same problem in both languages, but to
expect C programmers to wade though so much C++ stuff in order to
figure this out is more than you can reasonably hope for. Next time,
post such questions to comp.lang.c++.

...


When a file is newly opened, feof(fp) won't be true, not even if it's
a zero-length file, so there's no point in testing it until after the
first call to fread(). If the call to fread() sets the end-of-file
flag, the best you can hope for is that no data was read into your
structure. This appears to be what happened in your case; you
processed the results of the last fread() call, the one that tried and
failed to read the next record when there were no more records to
read. The structure happened to still have the same values in it from
the previous read, which is why it printed out one more time.

You shouldn't count on the consequences of such a mistake being so
minor. The worst that can happen is that the structure is only
partially filled, or filled in with random contents, in which case any
attempt to use it could backfire. Therefore, you should always check
for failure immediately after reading data, and process the data which
was read in only if the read was successful. A better approach:

      while(fread(&teu, sizeof teu, 1, fp) == 1)
      {
               // process data
      }
      if(ferr(fp))
      {
              // error handling
      }
      // else { end of file was reached }

What conditions would cause a structure to be only partially filed?
 
B

Ben Bacarisse

chad said:
On Jul 30, 4:25 pm, jameskuyper <[email protected]> wrote:

What conditions would cause a structure to be only partially filed?

If the data runs out part way through reading one of the structures,
or if a read error occurs at such a time, then fread will return 0 (in
this example) and the contents of 'teu' are indeterminate.
 
J

James Kuyper

To be more specific, the discussion started with a message posted by
Francis Glassborow to the comp.std.c thread titled "Standards,
regulations, code, laws? What?" on 2001-12-28. He was not so much
describing his own conclusions, but his understanding of BSI's
interpretation of discussions they had had with ISO.
No. The rules are quite clear that any reference that includes a date
refers only to that specific version. Only undated references (which
are rarely used) automatically refer to the latest version. At one time
the standard introductory text suggested that people might want to
investigate the applicability of more recent versions and reach private
agreements to do so, but that text no longer appears in the current
drafting rules.

Jun Woong informed me of that requirement on 2002-09-26 in comp.std.c.
Sorry that I forgot about it.
 

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

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top