473,405 Members | 2,141 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,405 software developers and data experts.

EOF (end of file) problem

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.

Jul 19 '05 #1
5 19954
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.
"Karlo Basic" <ka*****@email.si> wrote in message
news:bo*************@ID-212926.news.uni-berlin.de...
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.

Jul 19 '05 #2
On Sat, 01 Nov 2003 13:03:21 +0100, Moonlit wrote:
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;
}

Jul 19 '05 #3
Hi,

"Simon Saunders" <si************@net.ntl.com> wrote in message
news:pa****************************@net.ntl.com...
On Sat, 01 Nov 2003 13:03:21 +0100, Moonlit wrote:
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;
}

Oops, you are right, never noticed the fact that getline actually returns
the stream (stupid me :-( ).

Thanks for the tip.

Regards, Ron AF Greve
Jul 19 '05 #4
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
Jul 19 '05 #5
On 1 Nov 2003 07:58:03 -0800, Co**************@cox.net (Andrew EMA
Free!) wrote in comp.lang.c++:
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
Jul 19 '05 #6

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

Similar topics

7
by: Graham James Campbell CS2000 | last post by:
Having a nightmare problem with this and would appreciate any and all help. The situation is I want to move from a webform and format the user inputted text into some html I am storing in a...
10
by: NKOBAYE027 | last post by:
my friends, the end()... hehehe no...nothing so dire... I'm at an impasse I think. I've created the MathematicalSet class as follows - it has 4 lists. An ObjectPool of the actual objects, a...
4
by: James Aguilar | last post by:
Hey all, I'm working on an encoding scheme where I am running into a problem with reading a file off a stream. Looking at the binary encoding of the file (using a simple hex editor), there is...
8
by: dbuser | last post by:
Hi, I need help on a problem, as described below. I am reading a file "input.txt"which has data like this: abc def gh izk lmnopq rst uvwxyz I am using fstream object to read the file and...
12
by: Corey Burnett | last post by:
I have a client that has a split database (front-end/back-end). They are also using Access security - MDW file. The front end MDE file, the back end MDB file, and the MDW file are all located on...
5
by: rdemyan via AccessMonster.com | last post by:
I have code in my front end that opens a form for backing up the front end. I'll give a brief description of what the form does: 1) When the backup form opens, it closes all open forms except for...
2
by: Tammy | last post by:
Hello - I have read many threads regarding back-end security and have found them all useful. I have a couple of (what seem to be basic) questions: I have a secured front-end and back-end...
8
by: rdemyan via AccessMonster.com | last post by:
Anyone have any ideas on how to determine when the back-end file (containing only tables) has been updated with new data. The date/time of the file won't work because it gets updated to the...
6
by: LordZyse | last post by:
Just so everyone can know, I would LOVE to stay with yellowtip webserver, if you know how to install the GD files to it PLEASE let me know. This php worked on my 4.x php server but I get...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.