Endless string input

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
muthukumaran
New poster
Posts: 1
Joined: Tue Oct 25, 2005 8:50 am

Endless string input

Post by muthukumaran »

hi all,

i have a query.

how to get endless string inputs from user terminal until end of file is issued.
i tried the following piece of code. but it recognises EOF only when it is given as a fresh input in a new line

int main()
{
char a[81];

while( (fgets(a,80,stdin)) != NULL) // continue until EOF is issued
{
printf("%s",a);
/*
further processing
*/
}
return 0;
}

can someone plz clarify my problem.

muthu
ayon
Experienced poster
Posts: 161
Joined: Tue Oct 25, 2005 8:38 pm
Location: buet, dhaka, bangladesh

Post by ayon »

i think we are never to handle something like this, EOF occuring at the end of any string. we can give EOF at the last of any string simply by ctrl+z to the stdin. but uva-judges write their inputs in a file. they never directly give EOF, but at the end of the file an EOF occurs. such as the file below:

abcdefg
sadasd
thrthyjh

is similar to the file

abcdefg
sadasd
thrthyjh
EOF

thus, i think EOF occurs only at the 1st of a line(though i am not sure).

and sorry for my poor english...
ishtiak zaman
----------------
the world is nothing but a good program, and we are all some instances of the program
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

That's not true. EOF occurs after the last character in the file, which might not be an EOL. Anyway, there are very few cases when it matters.
rafagiu
New poster
Posts: 12
Joined: Sat Sep 24, 2005 8:30 pm

Post by rafagiu »

Krzysztof Duleba wrote:That's not true. EOF occurs after the last character in the file, which might not be an EOL. Anyway, there are very few cases when it matters.
Yes. And, just to complement your answer, the fact that EOF occurs after the last character in a file does not mean the file contains an EOF character. This is a myth. There is no character which indicates the end of a file. EOF is a actually an out-of-bound value (usually -1) returned by functions that process input. When you type CTRL+Z on the terminal, you just makel it tell the program that the input is over, no EOF character is sent to the program. So there's no difference between file redirection and terminal typing.

scanf and getchar return EOF when the input is over. fgets and gets, however, return NULL. That's the only thing wrong with muthukumaran's program excerpt.
Post Reply

Return to “C”