473,320 Members | 1,856 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,320 software developers and data experts.

Pass an EOF before pressing enter

Hi all,

I'll get straight into it.

When I try to run the code:
.....
while (scanf("%c", &c) == 1)
printf("%c", c);
.....
I input "abcd" follows by an EOF(Ctrl + d) instead of pressing enter,
and the program prints "abcd" on the screen. And then I pass an EOF
signal the program ends.

My question is why doesn't the program end the first it encounters the
EOF after the "abcd"?
And also if I DO press enter after "abcd", do I pass the characters
'a', 'b', 'c', 'd', '\n' to the program?

Any suggestions are appreciated.

Dec 31 '06 #1
9 2927
Camellia wrote:
>
When I try to run the code:
....
while (scanf("%c", &c) == 1)
printf("%c", c);
....
I input "abcd" follows by an EOF(Ctrl + d) instead of pressing
enter, and the program prints "abcd" on the screen. And then I
pass an EOF signal the program ends.

My question is why doesn't the program end the first it encounters
the EOF after the "abcd"?
Because you may really want to input a ^d it is only an EOF signal
when presented at the beginning of a line. You probably had to hit
ENTER (or return) after the first ^d.
>
And also if I DO press enter after "abcd", do I pass the characters
'a', 'b', 'c', 'd', '\n' to the program?
Yes.

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee.
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Dec 31 '06 #2
CBFalconer wrote:
Camellia wrote:

When I try to run the code:
....
while (scanf("%c", &c) == 1)
printf("%c", c);
....
I input "abcd" follows by an EOF(Ctrl + d) instead of pressing
enter, and the program prints "abcd" on the screen. And then I
pass an EOF signal the program ends.

My question is why doesn't the program end the first it encounters
the EOF after the "abcd"?

Because you may really want to input a ^d it is only an EOF signal
when presented at the beginning of a line. You probably had to hit
ENTER (or return) after the first ^d.

And also if I DO press enter after "abcd", do I pass the characters
'a', 'b', 'c', 'd', '\n' to the program?

Yes.

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee.
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Thanks Chuck for helping us newbies, and others :)

I was noticing something like the above also happening as I am going
through C book (KR2) examples as well. After reading this email I was
experimenting with the following program:
#include <stdio.h>
main()
{
int c;
while ( (c = getchar()) != EOF) {
putchar(c);
}
}
While running in the bash shell (I also tried cshell), if I type
characters, and then enter button, it prints characters I just entered,
then if I ctrl-d, it gives me the shell prompt. If I enter characters,
then ctrl-d, it will print characters, but no shell prompt. I can
repeat the pattern over and over until I enter two consecutive ctrl-d,
which will then print chars then give me a prompt.

Jan 1 '07 #3
vlsidesign said:

<snip>
While running in the bash shell (I also tried cshell), if I type
characters, and then enter button, it prints characters I just entered,
then if I ctrl-d, it gives me the shell prompt. If I enter characters,
then ctrl-d, it will print characters, but no shell prompt. I can
repeat the pattern over and over until I enter two consecutive ctrl-d,
which will then print chars then give me a prompt.
The exact mechanics of how you tell a shell to signify end-of-input to a
running process are really a question for a group dedicated to your
platform (or, indeed, to your shell, if it's a many-shelled platform).

We generally get as far as saying "try ^D (or ^Z for DOS/Win32) and see what
happens", but an in-depth discussion of shells would be beyond the scope of
comp.lang.c.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 1 '07 #4
CBFalconer wrote:
Camellia wrote:
>When I try to run the code:
....
while (scanf("%c", &c) == 1)
printf("%c", c);
....
I input "abcd" follows by an EOF(Ctrl + d) instead of pressing
enter, and the program prints "abcd" on the screen. And then I
pass an EOF signal the program ends.

My question is why doesn't the program end the first it encounters
the EOF after the "abcd"?

Because you may really want to input a ^d it is only an EOF signal
when presented at the beginning of a line. You probably had to hit
ENTER (or return) after the first ^d.
This is not accurate and yet no-one seems to have corrected it yet. It's
a good illustration of why not to try to address system-specifics on
comp.lang.c.

<OT>
Ctrl-d never has the function of actually inputting a Ctrl-d character
(ASCII EOT) and so the reason it appears to only be an EOF signal when
presented at the beginning of a line has nothing to do with anyone
really wanting to input a Ctrl-d character.

Ctrl+d has the effect of an EOF signal only when the line buffer is
empty. The line buffer can become empty in at least three ways:
1. By pressing Enter, which sends the contents of the line
buffer followed by '\n' and clears the line buffer, or
2. By pressing Ctrl+d, which sends the contents of the line
buffer without appending '\n' and clears the line buffer.
3. By pressing Backspace enough times to delete the contents.

The basic function of Ctrl+d is "flush buffer". If you try to flush the
buffer when it is already empty, that results in a read of zero bytes,
which is interpreted as an end-of-file condition.
</OT>

--
Simon.
Jan 2 '07 #5
Simon Biber <ne**@ralmin.ccwrites:
[...]
This is not accurate and yet no-one seems to have corrected it
yet. It's a good illustration of why not to try to address
system-specifics on comp.lang.c.
Indeed. Here's another illustration:
<OT>
Ctrl-d never has the function of actually inputting a Ctrl-d character
(ASCII EOT)
It does in raw mode, or following a Ctrl-V, or if the EOF character is
configured (by the "stty" command) to something other than Ctrl-D.

[...]
</OT>
And if I'm mistaken, it just proves the point even further. 8-)}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 2 '07 #6
Simon Biber wrote:
CBFalconer wrote:
Camellia wrote:
When I try to run the code:
....
while (scanf("%c", &c) == 1)
printf("%c", c);
....
I input "abcd" follows by an EOF(Ctrl + d) instead of pressing
enter, and the program prints "abcd" on the screen. And then I
pass an EOF signal the program ends.

My question is why doesn't the program end the first it encounters
the EOF after the "abcd"?
Because you may really want to input a ^d it is only an EOF signal
when presented at the beginning of a line. You probably had to hit
ENTER (or return) after the first ^d.

This is not accurate and yet no-one seems to have corrected it yet. It's
a good illustration of why not to try to address system-specifics on
comp.lang.c.

<OT>
Ctrl-d never has the function of actually inputting a Ctrl-d character
(ASCII EOT) and so the reason it appears to only be an EOF signal when
presented at the beginning of a line has nothing to do with anyone
really wanting to input a Ctrl-d character.

Ctrl+d has the effect of an EOF signal only when the line buffer is
empty. The line buffer can become empty in at least three ways:
1. By pressing Enter, which sends the contents of the line
buffer followed by '\n' and clears the line buffer, or
2. By pressing Ctrl+d, which sends the contents of the line
buffer without appending '\n' and clears the line buffer.
3. By pressing Backspace enough times to delete the contents.

The basic function of Ctrl+d is "flush buffer". If you try to flush the
buffer when it is already empty, that results in a read of zero bytes,
which is interpreted as an end-of-file condition.
</OT>

--
Simon.
Thanks for clearing it up. I'm a newbie going through the famous
Kernighan & Richie C book, and I was very curious about that behavior
as well. I guess a question like this should perhaps be posted in a
group for the bash or C shell that we are running the C program in,
right??

Jan 2 '07 #7
Simon Biber wrote:
CBFalconer wrote:
.... snip ...
>>
Because you may really want to input a ^d it is only an EOF signal
when presented at the beginning of a line. You probably had to hit
ENTER (or return) after the first ^d.

This is not accurate and yet no-one seems to have corrected it yet.
It's a good illustration of why not to try to address
system-specifics on comp.lang.c.

<OT>
Ctrl-d never has the function of actually inputting a Ctrl-d
character (ASCII EOT) and so the reason it appears to only be an
EOF signal when presented at the beginning of a line has nothing to
do with anyone really wanting to input a Ctrl-d character.

Ctrl+d has the effect of an EOF signal only when the line buffer is
empty. The line buffer can become empty in at least three ways:
1. By pressing Enter, which sends the contents of the line
buffer followed by '\n' and clears the line buffer, or
2. By pressing Ctrl+d, which sends the contents of the line
buffer without appending '\n' and clears the line buffer.
3. By pressing Backspace enough times to delete the contents.

The basic function of Ctrl+d is "flush buffer". If you try to flush
the buffer when it is already empty, that results in a read of zero
bytes, which is interpreted as an end-of-file condition.
</OT>
Your answer is at least as system specific as mine. The point is
that it normally is only used as an EOF signal at the beginning of
a line. Same for the use of ^z under DOS/Windows. The HP3000 used
':', or could be set to insist on ":EOF". After which regaining
your terminal required operator intervention :-)

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Jan 2 '07 #8
"vlsidesign" <fo*****@gmail.comwrites:
Simon Biber wrote:
[snip]
>Ctrl+d has the effect of an EOF signal only when the line buffer is
empty. The line buffer can become empty in at least three ways:
[snip]
>
Thanks for clearing it up. I'm a newbie going through the famous
Kernighan & Richie C book, and I was very curious about that behavior
as well. I guess a question like this should perhaps be posted in a
group for the bash or C shell that we are running the C program in,
right??
<OT>
It's not a shell issue, it's an operating system issue.
comp.unix.programmer would be the place to ask about it (assuming you
don't find the information you need in your system's documentation,
which should be the first thing you try).
</OT>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 2 '07 #9
In article <45***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>>Because you may really want to input a ^d it is only an EOF signal
when presented at the beginning of a line. You probably had to hit
ENTER (or return) after the first ^d.
[...]
>Ctrl+d has the effect of an EOF signal only when the line buffer is
empty. The line buffer can become empty in at least three ways:
1. By pressing Enter, which sends the contents of the line
buffer followed by '\n' and clears the line buffer, or
2. By pressing Ctrl+d, which sends the contents of the line
buffer without appending '\n' and clears the line buffer.
3. By pressing Backspace enough times to delete the contents.
>Your answer is at least as system specific as mine.
But it has the advantage over yours of being correct, unless you were
referring to some non-unix system.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jan 2 '07 #10

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

Similar topics

3
by: Adonis Walmsley-McCarthy | last post by:
Dear all, Does anybody know how to disable the bypass key ("SHIFT") which is used at startup? Thanks in advance, Adonis.
10
by: mg | last post by:
I want to enter characters in a TextBox in a WebForm, press a Button in the WebForm, and read the characters that were typed into the TextBox from within the Button_Click event handler. The main...
2
by: Cindy | last post by:
Hi all you smarties out there, I'm having a little conundrum with my asp.net page Scenario: I have a form (asp.net) with no code behind (as yet). I have placed a javascript function on a...
4
by: roxanaislam | last post by:
Submitting Form by pressing the "ENTER" key -------------------------------------------------------------------------------- Hi: I have a search form in my application which has 4 dropdown...
1
by: Camellia | last post by:
Hi all, I'll get straight into it. When I try to run the code: ..... while (scanf("%c", &c) == 1) printf("%c", c); ..... I input "abcd" follows by an EOF(Ctrl + d) instead of pressing...
4
by: nkoier | last post by:
Hi, I've been going crazy trying to figure out what's wrong with our Asp.Net 2.0 intranet site. At the very top of our main page I provide a TextBox and a Button for submitting Google searches....
3
by: Jake Barnes | last post by:
I'm researching the Enter key. This is for an Ajax chat application. The designer tells me that she wants people to be able to submit text simply by hitting the Enter key. She wants this to happen...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.