Newbie Question: EOF in MS Visual Studio 2003?

E

entropy123

Hey all,

A newbie, I am working through Kernighan and Ritchie.

I'm having a problem with one of their test programs & MS VS 2003. In
particular the EOF value for VS2003 in winxp is apparently '-1'
However when I enter '-1' as a character in this program the program
(which ought to terminate when EOF is input) will not quit. I need to
hit 'ctrl-c' to exit the program ----> no output...

Thanks for your help,
entropy

Here is the program:

#include <stdio.h>

int main()
{
int c, nl;

nl = 0;
while (( c = getchar() ) != EOF)
if ( c == '\n')
++nl;
printf("%d\n", nl);
}
 
J

Joona I Palaste

entropy123 said:
A newbie, I am working through Kernighan and Ritchie.
I'm having a problem with one of their test programs & MS VS 2003. In
particular the EOF value for VS2003 in winxp is apparently '-1'
However when I enter '-1' as a character in this program the program
(which ought to terminate when EOF is input) will not quit. I need to
hit 'ctrl-c' to exit the program ----> no output...
Thanks for your help,
entropy

You have misunderstood what EOF is. It is *not* a character. It does
not exist in the stream at all. It is an extra-special return code from
getchar(). It means "due to some implementation-specific magic, the
stream closed up. I'll pull this -1 thingy out of my hat to make up for
it." Really. That's all it means.
If you want to tell the program yourself that there will be no more
data, you have to use some implementation-specific magic yourself. On
Windows, this involves typing ctrl-Z. On Unix, it is ctrl-D. On Amiga,
it is ctrl-\. I don't know about other platforms.
Here is the program:
#include <stdio.h>
int main()
{
int c, nl;
nl = 0;
while (( c = getchar() ) != EOF)
if ( c == '\n')
++nl;
printf("%d\n", nl);
}

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Immanuel Kant but Genghis Khan."
- The Official Graffitist's Handbook
 
L

Lew Pitcher

Joona said:
You have misunderstood what EOF is. It is *not* a character. It does
not exist in the stream at all. It is an extra-special return code from
getchar(). It means "due to some implementation-specific magic, the
stream closed up. I'll pull this -1 thingy out of my hat to make up for
it." Really. That's all it means.
If you want to tell the program yourself that there will be no more
data, you have to use some implementation-specific magic yourself. On
Windows, this involves typing ctrl-Z. On Unix, it is ctrl-D.

Or not ;-)

/If/ the stream is redirected from a file, then the physical act of
attempting to read beyond the last byte of data invokes the passing of EOF.

OTOH, if the stream is read from a 'character special device' (i.e., a
terminal or similar), the OS interprets (or not) the characters, and
delivers the metadata signal of "reading beyond the last byte of data" based
on the character read. Since this character is configurable, it /can/ be
ctrl-D, but doesn't /have to be/ ^D. Indeed, if the device is conditioned to
be deliver data in 'raw' mode (admittedly, a condition and operation that's
off-topic in CLC), the system may /never/ signal that an end-of-data
condition has been encountered.

On Amiga,
it is ctrl-\. I don't know about other platforms.
[snip]


--

Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
 
E

entropy123

You have misunderstood what EOF is. It is *not* a character. It does
not exist in the stream at all. It is an extra-special return code from
getchar(). It means "due to some implementation-specific magic, the
stream closed up. I'll pull this -1 thingy out of my hat to make up for
it." Really. That's all it means.
If you want to tell the program yourself that there will be no more
data, you have to use some implementation-specific magic yourself. On
Windows, this involves typing ctrl-Z. On Unix, it is ctrl-D. On Amiga,
it is ctrl-\. I don't know about other platforms.
Joona,

Thanks for your help. It certainly is ctrl-z. I can understand why a
book last revised in 1989 might not have the info...but not in the VS
2003 help files....

entropy
 
I

istartedi

entropy123 said:
Joona,

Thanks for your help. It certainly is ctrl-z. I can understand why a
book last revised in 1989 might not have the info...but not in the VS
2003 help files....

I think they were assuming that the target audience was familiar with the
DOS command-line. I'm not trying to insult you, but I think you need to
learn more about the command-line. Google for some tutorials. It feels
funny for me to say this, but I have to say don't waste your time learning
too much about the DOS/Windows CLI. The *NIX shells seem to be where the
future is at as far as CLIs are concerned, and you can run them on Windows
(easiest way is via Cygwin) if you want to practice without partitioning
your hard drive.

Raise your hand if you think "knows all the ins and outs of the DOS/Windows
CLI tools and batch file programming" is going to sound a lot like "COBOL
programmer" in a few years. Actually, it already does to me.

--$teve
 
A

Arthur J. O'Dwyer

Raise your hand if you think "knows all the ins and outs of the DOS/Windows
CLI tools and batch file programming" is going to sound a lot like "COBOL
programmer" in a few years. Actually, it already does to me.

Batch-file programming is definitely old-skool in my mind, but I have to
say that my life at the moment would be a lot more tedious *without* batch
files. DOS command-line tools: well, I *was* going to say that I use
them all the time, until I realized that most of what I use regularly is
really the DJGPP textutils set, and not DOS at all - cat, less, diff,...
I hardly ever use type, more, or fc anymore. And I assume you're not
going to count del, deltree, dir, copy, mkdir, move, or ren. For a speedy
typer, they're a heck of a lot simpler than the Windows file system
interface. :)

-Arthur
 
E

entropy123

I think they were assuming that the target audience was familiar with the
DOS command-line. I'm not trying to insult you, but I think you need to
learn more about the command-line. Google for some tutorials. It feels
funny for me to say this, but I have to say don't waste your time learning
too much about the DOS/Windows CLI. The *NIX shells seem to be where the
future is at as far as CLIs are concerned, and you can run them on Windows
(easiest way is via Cygwin) if you want to practice without partitioning
your hard drive.

Raise your hand if you think "knows all the ins and outs of the DOS/Windows
CLI tools and batch file programming" is going to sound a lot like "COBOL
programmer" in a few years. Actually, it already does to me.

--$teve

$teve,

No, not insulted at all. I'm famiiar with *NIX shells and their
command lines, but just had no clue in winxp. :) One of my many
misconceptions had to do with "winxp EOF" searches that yielded little
or poor results...

I agree I need to look into explicit DOS command line commands....at
least for the time being...

entropy
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top