EOF question

N

newby2c

Greetings all,

Here is another incredibly silly question. I feel foolish asking it, but
i've no other choice. I was going through K&R2 and at section 1.5.2
(Character Counting), I decided to try out the program:

/* count characters in input; 1st version */

#include <stdio.h>

int main(void)
{
long nc;

nc=0;
while(getchar()!=EOF)
++nc;
printf("%ld\n",nc);
return 0;
}

after compiling it, I ran it. I typed in some letters and numbers and they
were repeated on the screen (as expected). But then what? How do I get the
count of the characters typed? I tried ctrl-d (I get a diamond), ctrl-z
(program terminates), and ctrl-c (program terminates). What am I doing wrong
or what am I understanding wrong? Any help is much appreciated.

Best,
newby2c
 
N

newby2c

[ snip ]

Sorry, I forgot to mention that I using a win98 box (if that even matters).

newby2c
 
P

pete

newby2c said:
[ snip ]

Sorry, I forgot to mention that I using a win98 box
(if that even matters).

Start your program, type your line,
then hit the enter key,
then your EOF, which is ctrl-z on my machine,
then hit the enter key again.
 
R

Richard Bos

(e-mail address removed) wrote:

[ Do not top-post. Corrected.
Have u tried the input -1 ??????

May I ask whatever gave you the idea that this could be useful? All it
does is return, subsequently, '-', '1', ' ', and six times '?' (rather
excessive, IYAM). Since all of these are in the required execution
character set, this input is in fact guaranteed _not_ to end the loop.

What the OP need to do (as usual, *sigh*), is to RTFFAQ. This question
is, unfortunately, not currently in the web version, so I can't link to
it (Steve: I think it should be, this is _very_ frequent), but it _is_
in the version which is posted here regularly; it's question 12.1b.

Richard
 
K

Keith Thompson

newby2c said:
/* count characters in input; 1st version */

#include <stdio.h>

int main(void)
{
long nc;

nc=0;
while(getchar()!=EOF)
++nc;
printf("%ld\n",nc);
return 0;
}

after compiling it, I ran it. I typed in some letters and numbers and they
were repeated on the screen (as expected). But then what? How do I get the
count of the characters typed? I tried ctrl-d (I get a diamond), ctrl-z
(program terminates), and ctrl-c (program terminates). What am I doing wrong
or what am I understanding wrong? Any help is much appreciated.

The program should read input characters until it hits an EOF
condition. How that EOF condition is triggered is system-specific.

You mentioned elsethread that you're running on a Windows system. You
should be able to trigger and EOF by entering control-Z on a line by
itself. The program should then print the number of characters
entered and terminate. (Perhaps you didn't notice the number?) You
should also be able to redirect the program's input from a text file;
EOF will then be triggered when the program reaches the end of the
file. Other systems will behave differently (which makes this
paragraph mildly off-topic).
 
K

Keith Thompson

newby2c said:
[ snip ]

Sorry, I forgot to mention that I using a win98 box (if that even matters).

newby2c

Have u tried the input -1 ??????

Please don't top-post. Your response should follow any quoted text,
not precede it.

I'm afraid you suggestion doesn't make any sense. EOF (which is
typically equal to -1, but it doesn't have to be) is the value
returned by getchar() when it reaches the end of the input. It's not
a character value. Even if it were, entering "-1" would cause
getchar() to return '-' followed by '1'.
 
E

Emmanuel Delahaye

newby2c wrote on 10/08/04 :
/* count characters in input; 1st version */

#include <stdio.h>

int main(void)
{
long nc;

nc=0;
while(getchar()!=EOF)
++nc;
printf("%ld\n",nc);
return 0;
}

after compiling it, I ran it. I typed in some letters and numbers and they
were repeated on the screen (as expected). But then what? How do I get the
count of the characters typed? I tried ctrl-d (I get a diamond),

This would be OK on a Unixoid machine.
ctrl-z
(program terminates),

This is OK on a DOS-Windows machine. You are probably using Dev-C++,
and the Dos-box close instantly. Just add a 'pause' before the return
of main():

getchar();
and ctrl-c (program terminates).

Don't do that.
 
C

CBFalconer

newby2c said:
.... snip ...

#include <stdio.h>

int main(void)
{
long nc;

nc=0;
while(getchar()!=EOF)
++nc;
printf("%ld\n",nc);
return 0;
}

You get no special prizes for eliding blanks. The following is
the same program, but legible:

#include <stdio.h>

int main(void)
{
long nc;

nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n", nc);
return 0;
}

Admittedly, there are very few added blanks, but get in the habit.
after compiling it, I ran it. I typed in some letters and numbers
and they were repeated on the screen (as expected). But then what?
How do I get the count of the characters typed? I tried ctrl-d (I
get a diamond), ctrl-z (program terminates), and ctrl-c (program
terminates). What am I doing wrong or what am I understanding
wrong? Any help is much appreciated.

The ctrl-z caused the system to generate EOF, execute the printf,
and terminate. The fact that you are confused makes me suspect
you are using some form of IDE, rather than a command line
compiler. The IDE is closing the window when (after) the program
exits.

Compile to disk (see your compiler systems documentation) and then
run the resultant program. Or, better, learn to use the command
line for compilation and running. This usually means (on windows
systems) using a dosbox. Some IDEs have a way to show the user
window after the program exits, Borlands among them.
 
N

newby2c

Richard Bos said:
What the OP need to do (as usual, *sigh*), is to RTFFAQ. This question
is, unfortunately, not currently in the web version, so I can't link to
it (Steve: I think it should be, this is _very_ frequent), but it _is_
in the version which is posted here regularly; it's question 12.1b.

Richard

Thank you. I will look for it when it is posted again.

newby2c
 
N

newby2c

pete said:
Start your program, type your line,
then hit the enter key,
then your EOF, which is ctrl-z on my machine,
then hit the enter key again.

Thanks. I tried that. Here is what happens:

I run the program.
I type in some words and use spaces and tabs.
I hit enter key.
Caret starts at new line.
I then hit ctrl-z.
Nothing happens!
I hit ctrl-z again and the program terminates, but the DOS box remains open.

newby2c
 
N

newby2c

Keith Thompson said:
The program should read input characters until it hits an EOF
condition. How that EOF condition is triggered is system-specific.

You mentioned elsethread that you're running on a Windows system. You
should be able to trigger and EOF by entering control-Z on a line by
itself. The program should then print the number of characters
entered and terminate. (Perhaps you didn't notice the number?) You
should also be able to redirect the program's input from a text file;
EOF will then be triggered when the program reaches the end of the
file. Other systems will behave differently (which makes this
paragraph mildly off-topic).

Thank you. But even ctrl-z on it's own line does not work. The first time I
enter ctrl-z, nothing happens. If I do ctrl-z a second time, the program
terminates with the DOS box remaining open.

newby2c
 
N

newby2c

[ snip ]
This is OK on a DOS-Windows machine. You are probably using Dev-C++,
and the Dos-box close instantly. Just add a 'pause' before the return
of main():

getchar();


Don't do that.

Thanks for the suggestion. Yes, I am using Dev-C++. However I do not use the
IDE to run my programs. I run everything from the command line in a DOS
window. Unfortunately, your suggestion did not work. I get the same results
as mentioned in my previously replies to others in this thread.

newby2c
 
N

newby2c

CBFalconer said:
You get no special prizes for eliding blanks. The following is
the same program, but legible:

#include <stdio.h>

int main(void)
{
long nc;

nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n", nc);
return 0;
}

Admittedly, there are very few added blanks, but get in the habit.

Thanks for the advice.
The ctrl-z caused the system to generate EOF, execute the printf,
and terminate. The fact that you are confused makes me suspect
you are using some form of IDE, rather than a command line
compiler. The IDE is closing the window when (after) the program
exits.

No. I do indeed use the command line from a DOS window to run programs.
Compile to disk (see your compiler systems documentation) and then
run the resultant program. Or, better, learn to use the command
line for compilation and running. This usually means (on windows
systems) using a dosbox. Some IDEs have a way to show the user
window after the program exits, Borlands among them.

See above.

newby2c
 
E

Emmanuel Delahaye

newby2c wrote on 10/08/04 :
[ snip ]
This is OK on a DOS-Windows machine. You are probably using Dev-C++,
and the Dos-box close instantly. Just add a 'pause' before the return
of main():

getchar();
I am using Dev-C++. However I do not use the
IDE to run my programs. I run everything from the command line in a DOS
window. Unfortunately, your suggestion did not work. I get the same results
as mentioned in my previously replies to others in this thread.

I think you must hit <ctrl-z> <enter>
 
C

CBFalconer

newby2c said:
.... snip ...

Thank you. But even ctrl-z on it's own line does not work. The
first time I enter ctrl-z, nothing happens. If I do ctrl-z a
second time, the program terminates with the DOS box remaining
open.

That sounds like a fault in the system, probably Microsofts. Try
ctrl-z return.
 
R

RoSsIaCrIiLoIA

Greetings all,

Here is another incredibly silly question. I feel foolish asking it, but
i've no other choice. I was going through K&R2 and at section 1.5.2
(Character Counting), I decided to try out the program:

/* count characters in input; 1st version */

#include <stdio.h>

int main(void)
{
long nc;

nc=0;
while(getchar()!=EOF)
++nc;
printf("%ld\n",nc);
return 0;
}

after compiling it, I ran it. I typed in some letters and numbers and they
were repeated on the screen (as expected). But then what? How do I get the
count of the characters typed? I tried ctrl-d (I get a diamond), ctrl-z
(program terminates), and ctrl-c (program terminates). What am I doing wrong
or what am I understanding wrong? Any help is much appreciated.

Best,
newby2c
Here in windows xp <ctrl-z>==EOF so if I push some chars <ctrl-z> and
'\n' the prog print the correnct number of char (less the last '\n').
I think you could resolve your problem if you write in the code
printf("%ld\n\n", nc); /* note \n\n */
and use <ctrl-z> for end.
 
N

newby2c

CBFalconer said:
That sounds like a fault in the system, probably Microsofts. Try
ctrl-z return.

Hiya CB,

I just tried your suggestion. No good.
Ran prog.
typed Hello, my name is newbyc (then ctrl-z on the same line).
Then hit enter.
Prog. terminates with DOS window still open (I added getchar();).
See code below:

/* count characters in input; 1st version */

#include <stdio.h>

int main(void)
{
long nc;

nc=0;
while(getchar()!=EOF)
++nc;
printf("%ld\n",nc);
getchar();
return 0;
}
 
N

newby2c

Emmanuel Delahaye said:
I think you must hit <ctrl-z> <enter>

Hi Emmanuel,

If you mean to hit <ctrl-z> *and* <enter> at the *same time*, I tried that.
Unfortunately, that did not work either.
 

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

Similar Threads

question 20
Question about the getchar() and eof !!!... 25
basic c i/o and EOF 1
basic c i/o and EOF 28
EOF in Windows 11
What's wrong ? 21
Pass an EOF instead of pressing enter 1
Pass an EOF before pressing enter 9

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top