Ben Bacarisse
View profile
- Hide quoted text -- Show quoted text -> Back to business, i found
this code there (for those who can read > french...and c) :
http://www.siteduzero.com/tutoriel-3-35363-realiser-des-saisies-secur...
#include <stdio.h> > #include <string.h> > static void purger(void)
{ > int c ; > while ( ( c = getchar() ) != '\n' && c !=
EOF ) ; > } > static void clean(char *chaine) > { > char *p =
strchr(chaine, '\n') ; > if (p) > *p = 0 ; > else
purger() ; > } > int main(void) > { > char chaine
[20] ; > printf("\n\nEnter some text : \n") ; > fgets(chaine,
sizeof chaine, stdin) ; > clean(chaine) ; > printf("\nThe text
entered is : '%s' ", chaine) ; > return 0 ; > } > The code above is
about cleaning the buffer from '\n' until EOF ( EOF > = CTRL-Z or D ?)
When there is no more input, getchar returns EOF (a negative integer)
to tell you that there is not more input. When you are typing at a
program you can signal that there is to be no more input using the
keyboard, but it is often configurable and it varies from system to
system. Ctrl+Z and Ctrl+D are common. > I understand broadly the
buffer is about memory managment and is > closer to computer
instructions from the processor than RAM. Anything > can go into the
buffer : video streaming, printer jobs. That is so general it won't
help you here. This is all about an input buffer. Most systems let
you prepare a line and send it to your program when you hit return.
This lets you correct errors before the program sees it. The code you
show is all about reading to the end of a line (usually one full input
buffer). > Fgets unlike scanf has a limit in the number of caracters
entered ? > However, like scanf, fgets reads and holds '\n'. I'm not
sure of the > last one, if someone could confirm it would help. fgets
does (read and store the newline). scanf is very general and can do
either. > while ( ( c = getchar() ) != '\n' && c != EOF ) ; > tells
to read any newline left (here it would be located in array > "chaine"
in main) after the user has entered more or less 20 > characters and
press ENTER... So, fgets reads <ENTER> or '\n' or EOF > if greater
than 20 characters and keeps it in the buffer? So in the > next entry,
if '\n' is in left in the buffer, the code will skip the > user's
entry. > Then the instruction getchar() tells to grab anything left in
the > buffer but '\n', assigns it to a variable c and in the meantime
somehow creates a new buffer that could hold any characters but >
'\n'. This sounds wrong though it may simply be that you are having
trouble saying it in English. The page you link to explained it all
quote well, I thought, so I am wary of trying to do better in a
language that is not your own. - Hide quoted text -- Show quoted text -
char *p = strchr(chaine, '\n') ; > strchr returns the position of
'\n' in chaine to *p . As i understand > from reading about strchr in
webpages, char *p is an array of > characters ? And in this code it
will only hold one value : '\n'. > if (p) > It tells to proceed if p
is anything but empty or null. And if it's > not null, it assigns 0 in
place of '\n'. > purger() > If *p is null, then it looks to empty the
buffer from the presence of > '\n' and to do so purger() is called. >
I'm not sure if what i understand is correct. I think this code is a >
milestone for manipulating text entries and i intend to use it from >
now on unless there is something better. > I feel sleepy and i hope
what i have written makes some sense. It is easier to explain
backwards rather than in the order you show the code. The user types
some text. You read it using fgets. If it fits (i.e. the input is
not too long) then strchr finds a \n in the array which gets replaced
by the string terminator (0). If the line is too long for the array,
then strchr finds no \n, but you (the user) have typed more characters
than have been read, so the program reads and that are left (i.e. up
to the \n that fgets never saw). This is only one way to read input.
It is useful in some programs and harmful in others. It all depends
on what the program is for. I'd suggest that throwing input away like
this is rarely the best thing to do, but it can help in simple
interactive programs. -- Ben.
(1 user) More options Aug 20, 3:41 am
Newsgroups: comp.lang.c
From: Ben Bacarisse <
[email protected]>
Date: Thu, 20 Aug 2009 02:41:10 +0100
Local: Thurs, Aug 20 2009 3:41 am
Subject: Re: Can C do it ?
Reply | Reply to author | Forward | Print | Individual message | Show
original | Report this message | Find messages by this author
<snip>
- Hide quoted text -
- Show quoted text -
Back to business, i found this code there (for those who can read
french...and c) :
http://www.siteduzero.com/tutoriel-3-35363-realiser-des-saisies-secur...
#include <stdio.h>
#include <string.h>
static void purger(void)
{
int c ;
while ( ( c = getchar() ) != '\n' && c != EOF ) ;
}
static void clean(char *chaine)
{
char *p = strchr(chaine, '\n') ;
if (p)
*p = 0 ;
else
purger() ;
}
int main(void)
{
char chaine[20] ;
printf("\n\nEnter some text : \n") ;
fgets(chaine, sizeof chaine, stdin) ;
clean(chaine) ;
printf("\nThe text entered is : '%s' ", chaine) ;
The code above is about cleaning the buffer from '\n' until EOF ( EOF
= CTRL-Z or D ?)
When there is no more input, getchar returns EOF (a negative integer)
to tell you that there is not more input. When you are typing at a
program you can signal that there is to be no more input using the
keyboard, but it is often configurable and it varies from system to
system. Ctrl+Z and Ctrl+D are common.
I understand broadly the buffer is about memory managment and is
closer to computer instructions from the processor than RAM. Anything
can go into the buffer : video streaming, printer jobs.
That is so general it won't help you here. This is all about an input
buffer. Most systems let you prepare a line and send it to your
program when you hit return. This lets you correct errors before the
program sees it. The code you show is all about reading to the end of
a line (usually one full input buffer).
Fgets unlike scanf has a limit in the number of caracters entered ?
However, like scanf, fgets reads and holds '\n'. I'm not sure of the
last one, if someone could confirm it would help.
fgets does (read and store the newline). scanf is very general and
can do either.
while ( ( c = getchar() ) != '\n' && c != EOF ) ;
tells to read any newline left (here it would be located in array
"chaine" in main) after the user has entered more or less 20
characters and press ENTER... So, fgets reads <ENTER> or '\n' or EOF
if greater than 20 characters and keeps it in the buffer? So in the
next entry, if '\n' is in left in the buffer, the code will skip the
user's entry.
Then the instruction getchar() tells to grab anything left in the
buffer but '\n', assigns it to a variable c and in the meantime
somehow creates a new buffer that could hold any characters but
'\n'.
This sounds wrong though it may simply be that you are having trouble
saying it in English. The page you link to explained it all quote
well, I thought, so I am wary of trying to do better in a language
that is not your own.
- Hide quoted text -
- Show quoted text -
char *p = strchr(chaine, '\n') ;
strchr returns the position of '\n' in chaine to *p . As i understand
from reading about strchr in webpages, char *p is an array of
characters ? And in this code it will only hold one value : '\n'.
if (p)
It tells to proceed if p is anything but empty or null. And if it's
not null, it assigns 0 in place of '\n'.
purger()
If *p is null, then it looks to empty the buffer from the presence of
'\n' and to do so purger() is called.
I'm not sure if what i understand is correct. I think this code is a
milestone for manipulating text entries and i intend to use it from
now on unless there is something better.
I feel sleepy and i hope what i have written makes some sense.
It is easier to explain backwards rather than in the order you show
the code. The user types some text. You read it using fgets. If it
fits (i.e. the input is not too long) then strchr finds a \n in the
array which gets replaced by the string terminator (0). If the line
is too long for the array, then strchr finds no \n, but you (the user)
have typed more characters than have been read, so the program reads
and that are left (i.e. up to the \n that fgets never saw).
This is only one way to read input. It is useful in some programs and
harmful in others. It all depends on what the program is for. I'd
suggest that throwing input away like this is rarely the best thing to
do, but it can help in simple interactive programs.