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

P

Pete Wilson

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!
 
M

Mike Wahler

Pete Wilson said:
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
 
J

John Harrison

Pete Wilson said:
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
 
D

David Harmon

On 3 Apr 2004 07:14:00 -0800 in comp.lang.c++, (e-mail address removed) (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/[email protected]
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top