473,320 Members | 1,930 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,320 software developers and data experts.

Unusual Input with fgets( )

Hello I need to enter a string of the form abc[EOF] (a string of
characters followed by EOF)

#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);

/*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 ?
Jul 19 '08 #1
4 5015
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"
Jul 19 '08 #2
Keith Thompson wrote:
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").
Yeah ,I know this!
...snip...
>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.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 100

int main(void)
{
char arr[MAX];
printf("Enter The String :");

if( fgets(arr,MAX,stdin) == NULL )
{
fprintf(stderr,"Bad Input");
return 1;
}
puts("");
return 0;
}

Basically i need the user to enter a string to be parsed later on in the
program. If the user enters a simple EOF (ctrl z or ctrl d as the system
may be, it is easy to detect since fgets will return NULL and feof will
detect the EOF in the input stream)

I am facing the problem if i have an input of the form :
(Say on Windows):

abc^Z(enter key)
or
abc^Z(enter key)

In each of the two cases above i need to press a key again to continue
with the next line of the program .fgets does not return anything till i
press a key again.

So the input *becomes*

abc^Z(enter key)
(enter key)

/*further processing*/

I cannot control what a user enters when he is asked to. I can process
the data later on but how do i handle an input like this at runtime?
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/>.
Jul 19 '08 #3
Newbie wrote:

...snip...
I am facing the problem if i have an input of the form :
(Say on Windows):

abc^Z(enter key)
or
abc^Z(enter key)
This should read as abc^Zefg(enter key)
Jul 19 '08 #4
Newbie <ne****@gmail.comwrites:
Keith Thompson wrote:
>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").

Yeah ,I know this!
..snip...
[more snippage]
Basically i need the user to enter a string to be parsed later on in
the program. If the user enters a simple EOF (ctrl z or ctrl d as the
system may be, it is easy to detect since fgets will return NULL and
feof will detect the EOF in the input stream)
Right.
I am facing the problem if i have an input of the form :
(Say on Windows):

abc^Z(enter key)
or
abc^Z(enter key)

In each of the two cases above i need to press a key again to continue
with the next line of the program .fgets does not return anything till
i press a key again.

So the input *becomes*

abc^Z(enter key)
(enter key)
Right, because that's what the user entered.

Apparently windows uses control-Z at the beginning of a line to
trigger an end-of-file condition. Control-Z *not* at the beginning of
a line is just another control character, not treated specially. In
that context, it's no different than control-X or control-Y, and you
should probably treat it the same way.
/*further processing*/

I cannot control what a user enters when he is asked to. I can process
the data later on but how do i handle an input like this at runtime?
[...]

If you like, you can scan the input string for a control-Z character:

#define CONTROL_Z '\x1a'

How your program should react to that depends on your requirements.

Or you can check for *any* non-printable character, perhaps using
isprint() (don't forget to convert the argument to unsigned char).
Again, how you handle such input is up to you.

--
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"
Jul 19 '08 #5

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

Similar topics

7
by: MM | last post by:
Hi there, How can I change my code (below) so that I use an "input argument" to specify the file name of the input file? For example, if I compile the code and that the application then gets the...
2
by: der | last post by:
Hello all, I want to use fgets() to read lines. Now, if the user has entered more characters than it was supposed to, the next call to fgets() would read these characters, which I don't want to...
3
by: Fao, Sean | last post by:
Hello all, As stated in another message, it's been a long time since I've done any C coding and I'm not feeling comfortable that I'm doing this correctly. Basically, I'd like to verify that my...
16
by: lovecreatesbeauty | last post by:
/* When should we worry about the unwanted chars in input stream? Can we predicate this kind of behavior and prevent it before debugging and testing? What's the guideline for dealing with it? ...
8
by: ais523 | last post by:
I use this function that I wrote for inputting strings. It's meant to return a pointer to mallocated memory holding one input string, or 0 on error. (Personally, I prefer to use 0 to NULL when...
1
by: yohan610 | last post by:
i have to read the binary data of a file, then encrypt them according to a supplied algorithm...and then the obtained output has to be written to an output file...everything works ok, and there are...
7
by: CaptainnFungi | last post by:
Hi All, I am very new to C and have been working my way through a few C books with the aim of getting more knowledge in programming. However I have hit a wall and I am not sure how to get over it....
77
by: arnuld | last post by:
1st I think of creating an array of pointers of size 100 as this is the maximum input I intend to take. I can create a fixed size array but in the end I want my array to expand at run-time to fit...
27
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I have a fully-portable C program (or at least I think I do). It works fine on Windows, but malfunctions on Linux. I suspect that there's something I don't know about the standard input stream...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.