LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 05-13-2013, 08:41 AM   #1
ofbodur
LQ Newbie
 
Registered: May 2013
Location: Istanbul/Turkey
Distribution: Mageia 2
Posts: 5

Rep: Reputation: Disabled
how to enter EOF indicator as an input ?


Dear visitor,

I entered the C code in my text editor. When I execute it with gcc, then ./a.out,
I don't know what character or number to enter to the program, which the program will recognize as
End-of-File indicator and finish looping.

At the end, it will give me the number of characters of the string that I entered, which is "nc".

The code is shown below:

Code:
#include<stdio.h>

main()
   {
long nc;
nc=0;

while ( getchar() != EOF )
{
++nc;
}
printf("%ld\n", nc );
}
Any further help is muchly appreciated..

Last edited by ofbodur; 05-13-2013 at 08:54 AM. Reason: making it more explicit
 
Old 05-13-2013, 09:32 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Control+D
(at the beginning of a line)
 
Old 05-13-2013, 09:40 AM   #3
ofbodur
LQ Newbie
 
Registered: May 2013
Location: Istanbul/Turkey
Distribution: Mageia 2
Posts: 5

Original Poster
Rep: Reputation: Disabled
All right,
You got wright, but I think it is a long combination to use... Can we assign any other character or symbol to use it more practically in other cases? For example if I want to count the number of characters in a text file, other than using awk command, and excluding the spaces between words to the counted number -it is ignored in our case- how is that possible for the programme to know where to end?
 
Old 05-13-2013, 09:51 AM   #4
ofbodur
LQ Newbie
 
Registered: May 2013
Location: Istanbul/Turkey
Distribution: Mageia 2
Posts: 5

Original Poster
Rep: Reputation: Disabled
OKEY! I got a new code, belong to some other programmer: It is so useful ..

Code:
#include <stdio.h>
#define IN   1  /* inside a word */
#define OUT  0  /* outside a word */
/* count lines, words, and characters in input */
main()
   {
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while ((c = getchar()) != EOF) {
++nc;
if (c == ‘\n’)
++nl;
if (c == ‘ ‘ || c == ‘\n’ || c == ‘\t’)
state = OUT;
else if (state == OUT) {
state = IN;
++nw;
           }
       }
printf(“%d %d %d\n”, nl, nw, nc);
}
also answers my question from the beginning..

Last edited by ofbodur; 05-13-2013 at 10:27 AM.
 
Old 05-13-2013, 12:54 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
1. Control+D is only two keypress -- not that difficult
2. When processing actual files (not keyboard input), you don't have to do anything, your program will get EOF at the end of the file.
 
  


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
question about text/source files: what is the EOF indicator? rahul8705 Linux - General 2 11-10-2010 11:36 AM
EOF and input redirection in C on OS X problem vdogvictor Programming 11 05-18-2009 09:21 PM
File status indicator for EOF? steven.c.banks Linux - Newbie 5 12-27-2007 06:55 AM
Hanyu Pinyin input via Keyboard Indicator applet General Linux - Software 0 02-10-2007 02:30 PM
Can't enter input using debug in Kdevelop Alan Wright Programming 0 06-17-2004 04:03 PM

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

All times are GMT -5. The time now is 03:42 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