In article <11**********************@g49g2000cwa.googlegroups .com>,
<sa***********@yahoo.com> wrote:
This is in pure C, I have 3 options to read IP address, netmask and
gateway address, I need to display in menu, there is one config file
called addess.cfg, when the system boots, it read that file and
displays value, it will give options to user incase if he wants to
modify the value, if he don't want to modify, then he press return key.
I need to implement in C.
Ah, then your difficulty is not to detect that "the keyboard has been hit",
but rather your difficulty is to detect whether the user input line
was empty or had some meaningful data.
There is more than one way to proceed.
One way would be to read an input line (but NOT using gets()!) into a
buffer and then use sscanf() to scan the buffer. If there is no
data value in the string, then you will get either 0 or EOF returned
by sscanf(). The difficulty with this approach is that you have
to know what the maximum input line size is ahead of time, or else
use some mechanism to dynamically allocate the input line buffer as
you read in characters. Always assume that the user is malicious,
or at the very least assume that at some point the user will fall
asleep at the keyboard and end up inputing a looooong string of
spaces.
A method to get around the input line size issue is to loop around
fetching characters (e.g., getchar()), discarding spaces and tabs and
formfeeds and such, until you either encounter a newline or a
non-whitespace character.
- If it was a newline then the empty line was entered and you proceed
on that case.
- If the character is not one that can form part of valid input at that
point, then the user input is in error and you take appropriate action
to notify the user and flush to the end of the input line.
- If the character is one that can form part of valid input at that
point, you ungetc() the character back into the input stream, then
scanf() using an appropriate format... checking that the format
actually matched, and checking to be sure nothing extra was entered on
the input line. [A hint in this last regard: if the user entered valid
input and it was matched by one of the numeric format specifiers such
as %d, and if that was the last thing on the line, then the newline
will be left in the input stream, *not* "consumed" by the scanf().]
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers