LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-26-2008, 08:30 AM   #1
geraneum
LQ Newbie
 
Registered: Nov 2008
Posts: 3

Rep: Reputation: 0
Question Help with getchar() please


HI

I've just started to learn C and C++.

I write the code below but dont get desired answer. I want to count the characters but when I run it and type something, a new line begins and nothing happen...

Code:
#include <stdio.h>

main()
{
	int nc;

	nc = 0;
	while (getchar() != EOF)
		++nc;
	printf("%d",nc);
}
I'm compiling with Borland CodGear in Windows...

Thanks
 
Old 11-26-2008, 08:43 AM   #2
cmnorton
Member
 
Registered: Feb 2005
Distribution: Ubuntu, CentOS
Posts: 585

Rep: Reputation: 35
Test for EOF May Not Work

Look for The EOF pitfall

http://en.wikipedia.org/wiki/Fopen
 
Old 11-26-2008, 09:21 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
While cmnorton's link is probably a good read and the EOF pitfall is certainly a thing to keep in mind, your code is OK in that respect.

I would recommend changing the printf statement a little though:
Code:
printf("%d\n",nc);
The solution to your 'problem' is quite simple: your code is waiting (looping) until a End-Of-File (EOF) comes in from the standard input. And by pressing enter, you're not sending a EOF, but newline character.

Try pressing CTRL-d after typing a few characters. CTRL-d is the way to send EOF.

Or pipe some data from a file or commandline into your program. This will send EOF automatically:
Code:
shell$ cat /etc/fstab | ./yourprog
shell$ echo '123456' | ./yourprog

Last edited by Hko; 11-26-2008 at 09:24 AM.
 
Old 11-26-2008, 01:34 PM   #4
cmnorton
Member
 
Registered: Feb 2005
Distribution: Ubuntu, CentOS
Posts: 585

Rep: Reputation: 35
Question Forgot about CTRL-D

Quote:
Originally Posted by Hko View Post
While cmnorton's link is probably a good read and the EOF pitfall is certainly a thing to keep in mind, your code is OK in that respect.

I would recommend changing the printf statement a little though:
Code:
printf("%d\n",nc);
The solution to your 'problem' is quite simple: your code is waiting (looping) until a End-Of-File (EOF) comes in from the standard input. And by pressing enter, you're not sending a EOF, but newline character.

Try pressing CTRL-d after typing a few characters. CTRL-d is the way to send EOF.

Or pipe some data from a file or commandline into your program. This will send EOF automatically:
Code:
shell$ cat /etc/fstab | ./yourprog
shell$ echo '123456' | ./yourprog
Thanks. I forgot about generating a CTRL-D on stdin.
 
Old 11-27-2008, 11:48 AM   #5
geraneum
LQ Newbie
 
Registered: Nov 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Lightbulb Thanks

I understood your explanation.
I've thought about it that process is still in the loop. cause any code after the loop didn't execute. But I didn't know why...
Thank you even if I can't get result I know the solution now.

But when I write something in input and press Ctrl-D nothing happen but something like this: hello^D...

Thanks
 
Old 11-27-2008, 12:41 PM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by geraneum View Post
But when I write something in input and press Ctrl-D nothing happen but something like this: hello^D...
You need to have at least one newline (i.e.: press enter) before ctrl-d. Or pressing ctrl-d twice also worked on my computer, but then the number is printed right after the string I typed.

If that does not work, then your terminal (or shell?) probably does no recognize ctrl-d as EOF. Try another terminal (or shell?) (bash, xterm, konsole, whatever...)
 
Old 11-29-2008, 10:55 AM   #7
geraneum
LQ Newbie
 
Registered: Nov 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Lightbulb CMD and Bash EOF's

I've found it just now...
http://www.mhuffman.com/notes/dos/bash_cmd.htm
 
Old 11-30-2008, 01:32 PM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
ah,yes. I totally overlooked it was under Windows. Sorry.
 
Old 12-01-2008, 04:24 AM   #9
baig
Member
 
Registered: Nov 2008
Location: وادی ھنزہ
Distribution: Solaris 5.10, Debian Server 5.2, CentOS 5.6
Posts: 226
Blog Entries: 3

Rep: Reputation: 38
you have used getchar..

but from where you are getting those chars???

no file pointer present in your code..

define a file pointer to your file.. e.g

FILE *fp;

fp=fopen("yourfile","r");

while( fp!=EOF)
{

bla bla...

}
 
Old 12-18-2008, 04:42 AM   #10
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by baig View Post
you have used getchar..

but from where you are getting those chars???
From the standard input one would suspect..
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
getchar() satellite Programming 10 12-24-2017 01:47 PM
about getchar() captainstorm Programming 6 10-11-2003 04:14 AM
getchar() NSKL Programming 18 01-11-2003 10:19 AM
getchar() not working? lackluster Programming 3 06-24-2002 08:09 AM
getchar() Winter Programming 6 05-11-2002 01:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration