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

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 2767
"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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.