Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

End of File (EOF) problem in Windows

1 view
Skip to first unread message

Karlman

unread,
Oct 29, 2003, 10:13:04 AM10/29/03
to
Hello!
I have a problem with a C++ program under Windows but which works fine
under Linux. The program simply reads in integers from a file (source
code below):

ifstream in_file;
int number;
in_file.open("whatever");
while (!in_file.eof())
in_file >> number;

In Linux it reads all the numbers in the file no matter how EOF is
positioned: right after the last number or on the next line in
in_file. In Windows it won't read the last number unless there's an
empty line at the end of the file.
Example:
1 2 3 4 5 6 7EOF -> this works under Linux but not under Windows
1 2 3 4 5 6 7<CR>
EOF -> this works under both OS's
I use gcc 2.96 under Linux and Borland C++ 5.5.1 under Windows. Could
anyone help me with this one, please. I would appreciate it very much.

Karlo Basic

CBFalconer

unread,
Nov 2, 2003, 2:04:39 AM11/2/03
to

For C, and I believe C++ also, action without proper line
termination is implementation defined. From N869:

7.19.2 Streams

[#1] Input and output, whether to or from physical devices
such as terminals and tape drives, or whether to or from
files supported on structured storage devices, are mapped
into logical data streams, whose properties are more uniform
than their various inputs and outputs. Two forms of mapping
are supported, for text streams and for binary streams.209)

[#2] A text stream is an ordered sequence of characters
composed into lines, each line consisting of zero or more
characters plus a terminating new-line character. Whether
the last line requires a terminating new-line character is
implementation-defined. Characters may have to be added,
altered, or deleted on input and output to conform to
differing conventions for representing text in the host
environment. Thus, there need not be a one-to-one
correspondence between the characters in a stream and those
in the external representation. Data read in from a text
stream will necessarily compare equal to the data that were
earlier written out to that stream only if: the data consist |
only of printing characters and the control characters
horizontal tab and new-line; no new-line character is
immediately preceded by space characters; and the last
character is a new-line character. Whether space characters
that are written out immediately before a new-line character
appear when read in is implementation-defined.

____________________

209An implementation need not distinguish between text
streams and binary streams. In such an implementation,
there need be no new-line characters in a text stream nor
any limit to the length of a line.


--
Chuck F (cbfal...@yahoo.com) (cbfal...@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


0 new messages