Newbie <ne****@gmail.comwrites:
Hello I need to enter a string of the form abc[EOF] (a string of
characters followed by EOF)
I assume you mean end-of-file, not the characters 'E', 'O', 'F'.
The characters 'a', 'b', 'c' can of course be contained in a string.
EOF can't. EOF is a macro that expands to a constant expression with
a negative value (typically -1, but don't depend on that). It's a
value returned by functions like getchar() to indicate an end-of-file
condition. It's not a character value. If you try to store the value
EOF in a string, you can do it, but it will be converted from type int
to type char and will give you an ordinary character value (on most
systems that use an ASCII-derived character set, it's a lowercase 'y'
with two dots over it, what Unicode calls "LATIN SMALL LETTER Y WITH
DIAERESIS").
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 100
int main(void)
{
char arr[MAX];
puts("Enter String :");
fgets(arra,100,stdin);
You declared MAX; you should use it here:
fgets(arra, MAX, stdin);
/*Do Something*/
puts("");
return 0;
}
Is there any way i can do this ?The code i pasted above halts once an
EOF is entered.Is there any way to prevent the program from halting
for any further input ?
Actually this is a part of a bigger question where i am trying to
parse a string ( to validate user entered int/double etc )
A Simple EOF is easy to handle but if the EOF is entered after a set
of characters the program waits for a further input!!
What is the right way to handle such an input ?
It's hard to tell why your program halts, since you haven't shown us
the whole thing. The program you posted halts after attempting to
read a single line -- at least, it does once you fix the typo ("arra"
should be "arr"). It's always best to copy-and-paste your actual
code; if you post some approximation of it, it's impossible to tell
which problems are in your real code and which were introduced when
you summarized it.
For keyboard input, end-of-file is usually indicated by pressing some
system-specific sequence, typically control-D (for Unix-like systems)
or control-Z (for DOS/Windows-like systems). This is intended to
indicate that you've reached the end of your input, so trying to read
more input after that doesn't generally make much sense.
The actual behavior for interactive devices varies across systems, and
the intent of the standard is (arguably) unclear -- which emphasizes
the point that you should avoid depending on it.
Since end-of-file is a condition, different input functions respond to
it in different ways (getchar() returns EOF, fgets() returns a null
pointer, etc.). Consult the documentation for each function you want
to use (always a good idea regardless).
If you insist on trying to read input after reaching end-of-file, you
can call clearerr(stdin). Assuming this works, it means your program
will work differently reading from a file (where the end-of-file
condition will be set the next time you try to read from it, since
there just isn't anything more to read) than from an interactive
device such a keyboard.
It's better to design your program so that end-of-file doesn't occur
until you're actually done.
See also section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/>.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"