EOF (end of file) problem

K

Karlo Basic

Hi!
I hope I'm asking the right question in the right group.
I have a problem with a piece of C++ code that runs well under Linux
(gcc 2.96) but doesn't run as expected in Windows (Borland C++ 5.5.1):
Here's the code:
**********
int num;
ifstream in_file;
in_file.open("whatever.txt");
while (!in_file.eof())
cin >> num;
**********
Example input:
1 2 3 4 5 6 7EOF -> Linux OK; Windows not
1 2 3 4 5 6 7
EOF -> Linux & Windows OK
In Linux it reads right to the end of the file. But in Windows it misses
the last number (7) unless there's a newline character at the end of the
last line to be read. If I replace "int num" with "char num" the it
works as it should. What am I missing here?
Thanks in advance,
Karlo.
 
M

Moonlit

Hi,

This is the way I read files (both linux and windows).

ifstream Input( 'whatever.txt' );
if( Input.is_open() )
{
string Line;
while( Input )
{
getline( Input, Line );
if( Input )
{
cout << "Read <" << Line << ">" << endl;
}
}

}

Note the extra check after reading!

Regards, Ron AF Greve.
 
S

Simon Saunders

Hi,

This is the way I read files (both linux and windows).

ifstream Input( 'whatever.txt' );
if( Input.is_open() )
{
string Line;
while( Input )
{
getline( Input, Line );
if( Input )
{
cout << "Read <" << Line << ">" << endl;
}
}

}

Note the extra check after reading!

Regards, Ron AF Greve.

This is a little neater (avoids your extra check):

while (getline(Input, Line))
{
cout << "Read <" << Line << ">" << endl;
}
 
M

Moonlit

Hi,

Simon Saunders said:
This is a little neater (avoids your extra check):

while (getline(Input, Line))
{
cout << "Read <" << Line << ">" << endl;
}
Oops, you are right, never noticed the fact that getline actually returns
the stream (stupid me :-( ).

Thanks for the tip.

Regards, Ron AF Greve
 
A

Andrew EMA Free!

Well when reading in a file C++ Has no way of telling if it will read
in a char, int, ect... So it will return in char because this type of
variable can hold any letter, number, or symbol. So when reading in a
file read it into a char variable. This is why when you read in the
file using a char variable it works correctly.

BTW: Many people think that there is really a EOF symbol in a file..
This is NOT true. C++ Will return -1 when the file is complete read
in... EOF is a Marco declared in iostream.h for -1.

Andrew
 
J

Jack Klein

Well when reading in a file C++ Has no way of telling if it will read
in a char, int, ect... So it will return in char because this type of
variable can hold any letter, number, or symbol. So when reading in a
file read it into a char variable. This is why when you read in the
file using a char variable it works correctly.

BTW: Many people think that there is really a EOF symbol in a file..
This is NOT true. C++ Will return -1 when the file is complete read
in... EOF is a Marco declared in iostream.h for -1.

Andrew

The C++ standard inherits the macro EOF from the C language standard,
and does not specify or require that the value of this macro be -1,
although that is very common.

The only requirement is that EOF have type int and a value less than
0. It could be any value from -1 through INT_MIN inclusive.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top