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

getchar function and EOF problem..

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;
}

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

Mar 10 '06 #1
13 12365
"broeisi" <br*******@gmail.com> writes:
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.

--
"The lusers I know are so clueless, that if they were dipped in clue
musk and dropped in the middle of pack of horny clues, on clue prom
night during clue happy hour, they still couldn't get a clue."
--Michael Girdwood, in the monastery
Mar 10 '06 #2
"broeisi" <br*******@gmail.com> wrote in news:1142023302.474499.156390
@u72g2000cwu.googlegroups.com:
==============================================

#include <stdio.h>

int main(void)
{
char c;

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

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

return 0;
}


http://www.eskimo.com/~scs/c-faq.com.../getcharc.html
--
A. Sinan Unur <1u**@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
Mar 10 '06 #3
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;
}

Mar 10 '06 #4
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?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Mar 10 '06 #5
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

broeisi wrote:
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

iD8DBQFEEebKagVFX4UWr64RArInAJ0WZ0VLP8LJV7hYCxUxEW 7CNXO8dACgmffE
CfR9HcY2o2GBIftHavjjNOI=
=zvu6
-----END PGP SIGNATURE-----
Mar 10 '06 #6
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.

Mar 10 '06 #7
"broeisi" <br*******@gmail.com> wrote in news:1142025102.928683.318040
@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

--
A. Sinan Unur <1u**@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

Mar 10 '06 #8
In article <11**********************@z34g2000cwc.googlegroups .com>,
broeisi <br*******@gmail.com> wrote:
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
Mar 10 '06 #9
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

Mar 10 '06 #10
broeisi wrote:
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
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Mar 11 '06 #11
"A. Sinan Unur" <1u**@llenroc.ude.invalid> writes:
[...]
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>

--
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.
Mar 11 '06 #12
"Albert" <al*****************@gmail.com> writes:
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.

--
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.
Mar 11 '06 #13
On 10 Mar 2006 13:11:42 -0800, "broeisi" <br*******@gmail.com> wrote:
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
Mar 12 '06 #14

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

Similar topics

3
by: Cam | last post by:
Hi everyone, Before I answer to a (hopefully) helpful reply to this post, I have been rapped over the knuckles for 'top-posting' and I do not wish to be a learner poster who observes poor...
12
by: Emmanuel Delahaye | last post by:
Hi there, It is commonly accepted that a call to the getchar() function suspends the execution of the current program. I have not found any description of this behaviour in the standard (I may...
1
by: White Spirit | last post by:
I'm trying to use getchar() to read alphanumeric data as follows:- char input; /* Take a string of input and remove all spaces therein */ int j = 0; while ((input = getchar()) != '\n') { if...
6
by: Alan | last post by:
I am using Standard C compiled with GCC under Linux Fedora Core 4 When I run this program and enter a character at the prompt, I have to press the ENTER key as well. This gives me 2 input...
11
by: shekhardeodhar | last post by:
The program compiles properly (most of it is from k&r) but the second function (here character_count) gives wrong answer. Can someone please explain why ? #include<stdio.h> #define IN 1...
11
by: Michael Brennan | last post by:
If a call to getchar() returns EOF, is the behavior defined if another getchar() call is made? Will it return EOF again? The reason I'm asking: if I have a function that uses getchar()...
3
by: MargNat | last post by:
Hi, I've wrote a simple c++ program without graphic interface; Now, if I build it with Visual Studio function getchar() works in a way; if I build it with Dev-C++ function getchar() works in...
9
by: primeSo | last post by:
// FIRST int main(void){ int c, i = 0; char cArray; while( (c = getchar()) != '\n' && c != EOF){ cArray = c; i ++; }
22
by: arnuld | last post by:
Mostly when I want to take input from stdin I use getchar() but I get this from man page itself: "If the integer value returned by getchar() is stored into a variable of type char and then...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.