473,473 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

newb ques: c++ input files always end with NUL? Really??

C++ instructor says, emphatically: end of file is always a NUL char in
the file; count on it. When program sees a NUL, that's it, that's the
end of file. And testing for NUL is always the right thing to do.

Impossible to buy this because so diff from C; and because binary
files can't exist if it's true.

What is the right way to check portably for end of file?

Thanks!
Jul 22 '05 #1
6 2774
"Pete Wilson" <pe**@pwilson.net> wrote in message
news:5e**************************@posting.google.c om...
C++ instructor says, emphatically: end of file is always a NUL char in
the file; count on it.
He's wrong. (Unless he's talking about a particular file format
which has such a thing).

The last character in a file can be any character value at all
which is supported by the operating system's character set(s).

Some operating systems might use a particular character value
to indicate end of file, others might not. The C++ language
has nothing to say about this.
When program sees a NUL, that's it, that's the
end of file. And testing for NUL is always the right thing to do.
Nope, not in C++. The correct way is to check the stream state
of an fstream object, or for some C-style i/o functions, check
the return value for equality to the EOF macro.

Impossible to buy this because so diff from C; and because binary
files can't exist if it's true.
That's correct.

What is the right way to check portably for end of file?


Depends upon which functions you're using. Look up their
documentation.

You need to fire (or educate) your 'instructor'.

-Mike
Jul 22 '05 #2
"Pete Wilson" <pe**@pwilson.net> wrote in message
news:5e**************************@posting.google.c om...
C++ instructor says, emphatically: end of file is always a NUL char in
the file; count on it.
He's wrong. (Unless he's talking about a particular file format
which has such a thing).

The last character in a file can be any character value at all
which is supported by the operating system's character set(s).

Some operating systems might use a particular character value
to indicate end of file, others might not. The C++ language
has nothing to say about this.
When program sees a NUL, that's it, that's the
end of file. And testing for NUL is always the right thing to do.
Nope, not in C++. The correct way is to check the stream state
of an fstream object, or for some C-style i/o functions, check
the return value for equality to the EOF macro.

Impossible to buy this because so diff from C; and because binary
files can't exist if it's true.
That's correct.

What is the right way to check portably for end of file?


Depends upon which functions you're using. Look up their
documentation.

You need to fire (or educate) your 'instructor'.

-Mike
Jul 22 '05 #3

"Pete Wilson" <pe**@pwilson.net> wrote in message
news:5e**************************@posting.google.c om...
C++ instructor says, emphatically: end of file is always a NUL char in
the file; count on it.
Your instructor is utterly wrong. You could write a five line program to
prove it.
When program sees a NUL, that's it, that's the
end of file. And testing for NUL is always the right thing to do.

Impossible to buy this because so diff from C; and because binary
files can't exist if it's true.

What is the right way to check portably for end of file?
The ways that work in C also work in C++.

Assuming you are talking about the C++ iostream library then

fstream file("some_file");
while (file >> some_value)
{
}
if (file.eof())
{
// end of file
}
else
{
// some other sort of error
}

Note that eof is only tested after a read has failed. It does not reliably
predict that the next read will fail because of end of file. A common newbie
mistake is this

fstream file("some_file");
while (!file.eof())
{
file >> some_value;
}

which can end up going round the loop one too amny times.

john

Thanks!

Jul 22 '05 #4

"Pete Wilson" <pe**@pwilson.net> wrote in message
news:5e**************************@posting.google.c om...
C++ instructor says, emphatically: end of file is always a NUL char in
the file; count on it.
Your instructor is utterly wrong. You could write a five line program to
prove it.
When program sees a NUL, that's it, that's the
end of file. And testing for NUL is always the right thing to do.

Impossible to buy this because so diff from C; and because binary
files can't exist if it's true.

What is the right way to check portably for end of file?
The ways that work in C also work in C++.

Assuming you are talking about the C++ iostream library then

fstream file("some_file");
while (file >> some_value)
{
}
if (file.eof())
{
// end of file
}
else
{
// some other sort of error
}

Note that eof is only tested after a read has failed. It does not reliably
predict that the next read will fail because of end of file. A common newbie
mistake is this

fstream file("some_file");
while (!file.eof())
{
file >> some_value;
}

which can end up going round the loop one too amny times.

john

Thanks!

Jul 22 '05 #5
On 3 Apr 2004 07:14:00 -0800 in comp.lang.c++, pe**@pwilson.net (Pete
Wilson) wrote,
C++ instructor says, emphatically: end of file is always a NUL char in
the file; count on it. When program sees a NUL, that's it, that's the
end of file. And testing for NUL is always the right thing to do.


Wrong. Dump an exe file, you will see plenty of zeros.
http://groups.google.com/gr*********....earthlink.net

Jul 22 '05 #6
On 3 Apr 2004 07:14:00 -0800 in comp.lang.c++, pe**@pwilson.net (Pete
Wilson) wrote,
C++ instructor says, emphatically: end of file is always a NUL char in
the file; count on it. When program sees a NUL, that's it, that's the
end of file. And testing for NUL is always the right thing to do.


Wrong. Dump an exe file, you will see plenty of zeros.
http://groups.google.com/gr*********....earthlink.net

Jul 22 '05 #7

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

Similar topics

0
by: claudel | last post by:
Hi I have a newb PHP/Javascript question regarding checkbox processing I'm not sure which area it falls into so I crossposted to comp.lang.php and comp.lang.javascript. I'm trying to...
3
by: Andreas Winkler | last post by:
Hi I tried to have python call an external 'Word'-file, and the read the whole text into a single string with the following code: source = raw_input('file path') File = open('source', 'r')...
3
by: claudel | last post by:
Hi I have a newb PHP/Javascript question regarding checkbox processing I'm not sure which area it falls into so I crossposted to comp.lang.php and comp.lang.javascript. I'm trying to...
0
by: Pete Wilson | last post by:
C++ instructor says, emphatically: end of file is always a NUL char in the file; count on it. When program sees a NUL, that's it, that's the end of file. And testing for NUL is always the right...
24
by: Apotheosis | last post by:
The problem professor gave us is: Write a program which reads two integer values. If the first is less than the second, print the message "up". If the second is less than the first, print the...
11
by: The_Kingpin | last post by:
Hi all, I'm new to C programming and looking for some help. I have a homework project to do and could use every tips, advises, code sample and references I can get. Here's what I need to do....
10
by: action! | last post by:
this is my C Programming homework, It wants me to input 10! and output the follow result 10! ¡õ 10 * 9! ¡õ 9 * 8! ..................
4
by: SteveKlett | last post by:
I have a subset of form items that I need to perform different operations on (enable/disable, clear values, change style, etc) rather than hard code the IDs or names I would like to recursively...
6
by: GrispernMix | last post by:
//ques and and level order traversal file name: lab6_build_leaf_up.cpp Instructions:
7
by: Dave | last post by:
Hey there, having a bit of problem iterating through lists before i go on any further, here is a snip of the script. -- d = "a1 b1 c1 d1 e1 a2 b2 c2 d2 e2 a3 b3 c3 d3 e3 a4 b4 c4 d4 e4 a5 b5 c5...
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,...
1
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.