getchar function and EOF problem..

B

broeisi

Hello,

Can someone help me out with this one?

The following program should read characters till it met the EOF (-1)
on my pc.
I'm running linux and using the gcc compiler version 3.4.5.
But it doesn't.
Am I doing something wrong here?.

==============================================

#include <stdio.h>

int main(void)
{
char c;

printf("EOF has value: %d\n", EOF);

while((c = getchar()) != EOF)
putchar(c);

return 0;
}

===============================================
 
B

Ben Pfaff

broeisi said:
The following program should read characters till it met the EOF (-1)
on my pc.
I'm running linux and using the gcc compiler version 3.4.5.
But it doesn't.
Am I doing something wrong here?.

Yes. You failed to read the FAQ, which answers your question
directly:

12.1: What's wrong with this code?

char c;
while((c = getchar()) != EOF) ...

A: For one thing, the variable to hold getchar's return value must
be an int. getchar() can return all possible character values,
as well as EOF. By squeezing getchar's return value into a
char, either a normal character might be misinterpreted as EOF,
or the EOF might be altered (particularly if type char is
unsigned) and so never seen.

References: K&R1 Sec. 1.5 p. 14; K&R2 Sec. 1.5.1 p. 16; ISO
Sec. 6.1.2.5, Sec. 7.9.1, Sec. 7.9.7.5; H&S Sec. 5.1.3 p. 116,
Sec. 15.1, Sec. 15.6; CT&P Sec. 5.1 p. 70; PCS Sec. 11 p. 157.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hello,

Can someone help me out with this one?

The following program should read characters till it met the EOF (-1)
on my pc.
I'm running linux and using the gcc compiler version 3.4.5.
But it doesn't.
Am I doing something wrong here?.

You betcha. See below
==============================================

#include <stdio.h>

int main(void)
{
char c;
Note this.^

printf("EOF has value: %d\n", EOF);

while((c = getchar()) != EOF)
getchar() returns an int. You've stuffed the return value into a char,
and lost much significance in the data. Above, change
char c;
to
int c;
and see the difference.
putchar(c);

return 0;
}

===============================================



- --

Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEEebKagVFX4UWr64RArInAJ0WZ0VLP8LJV7hYCxUxEW7CNXO8dACgmffE
CfR9HcY2o2GBIftHavjjNOI=
=zvu6
-----END PGP SIGNATURE-----
 
B

broeisi

Well I changed the variable from char to int.
But still I have the same problem.

Here is the code:

#include <stdio.h>

int main(void)
{
int c;

printf("EOF has value: %d\n", EOF);

while((c = getchar()) != EOF)
putchar(c);

return 0;
}
 
R

Richard Heathfield

broeisi said:
Well I changed the variable from char to int.
But still I have the same problem.

Here is the code:

#include <stdio.h>

int main(void)
{
int c;

printf("EOF has value: %d\n", EOF);

while((c = getchar()) != EOF)
putchar(c);

return 0;
}

The code looks fine, so I suspect your problem is a misunderstanding of what
EOF actually means. How are you testing your code?
 
B

broeisi

I juist run the program, and keep entering characters.
When I want it to stop I enter -1...but it just echo it back.

I suppose that when I enter -1 it then equals EOF and would en the
while loop.
Am I mistaken here?

After all EOF is -1.
 
A

A. Sinan Unur

@z34g2000cwc.googlegroups.com:

[ You need to stat quoting some context ]
I juist run the program, and keep entering characters.
When I want it to stop I enter -1...but it just echo it back.

I suppose that when I enter -1 it then equals EOF and would en the
while loop. Am I mistaken here?

Absolutely.
After all EOF is -1.

-1 is the value getchar returns when it hits the end of file. For a
physical file, this is easy to determine.

From keyboard input, different shells have different conventions. On
bash, for example, entering a CTRL-D will signal end of input from the
console, whereas if one using cmd.exe on Windows, CTRL-Z would be used.

Sinan
 
R

Richard Tobin

I juist run the program, and keep entering characters.
When I want it to stop I enter -1...but it just echo it back.

I suppose that when I enter -1 it then equals EOF and would en the
while loop.
Am I mistaken here?

After all EOF is -1.

You'll probably find that A is 65, but typing "65" doesn't result in
an A being read. The integer value returned by getchar() to indicate
end-of-file has nothing to do with what you have to type to signal the
end of the input. Typically in unix-like systems the default thing
to type is control-D.

-- Richard
 
A

Albert

It looks like you want the program to output the value of EOF.
Try putting the printf statement after the while loop, but before the
return statement.

Albert
 
D

Default User

broeisi said:
I juist run the program, and keep entering characters.
When I want it to stop I enter -1...but it just echo it back.

See the .sig below for important Google information.
I suppose that when I enter -1 it then equals EOF and would en the
while loop.
Am I mistaken here?

After all EOF is -1.

You didn't enter an integer of value -1. You enter two characters, a
minus sign followed by the character 1. Those will be read separately
via your getchar() calls.

What you need is a way to signal EOF from the keyboard. That is
implementation-specific (if possible at all). Some common solutions are
control Z for DOS and Windows machines, or control D for UNIX.



Brian
 
K

Keith Thompson

A. Sinan Unur said:
From keyboard input, different shells have different conventions. On
bash, for example, entering a CTRL-D will signal end of input from the
console, whereas if one using cmd.exe on Windows, CTRL-Z would be used.

<OT>
For Unix-like systems, this is determined by a tty setting, not by the
shell.
</OT>
 
K

Keith Thompson

Albert said:
It looks like you want the program to output the value of EOF.
Try putting the printf statement after the while loop, but before the
return statement.

That makes no sense without context. Please read
<http://cfaj.freeshell.org/google/> to learn why you should provide
context when you post a followup, and how to do so despite Google's
determined efforts to make you look like a fool. (I'm flaming Google,
not you.)

Sadly, it makes no more sense with context. Here's the program
from the original post (I've deleted blank lines to save space):

#include <stdio.h>
int main(void)
{
char c;
printf("EOF has value: %d\n", EOF);
while((c = getchar()) != EOF)
putchar(c);
return 0;
}

The program is intended to print the value of EOF, and then copy stdin
to stdout. If you had tried it, you would have seen that it does
print the value of EOF.

Moving the printf after the while loop will accomplish nothing, and
has nothing to do with the code's actual problem. In fact, due to the
bug we've been discussing, it could prevent the printf() call from
ever being executed.
 
B

Barry Schwarz

I juist run the program, and keep entering characters.
When I want it to stop I enter -1...but it just echo it back.

I suppose that when I enter -1 it then equals EOF and would en the
while loop.
Am I mistaken here?

After all EOF is -1.

While the EOF macro may be defined as -1 on your system, the input
characters -1 do not represent end of file. They are simply the '-'
character followed by the '1' character. Also note that EOF is not a
character as such but the (status) value returned by the input
functions to indicate an unusual condition (incomplete or missing
input), such as error or end of file. As a matter of convention,
Windows systems use control-Z as the input character which causes the
input function to return EOF and Unix systems use control-D. You will
need to review your system specific documentation to determine which
value you should use.


Remove del for email
 

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,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top