Question 
Author Message
 Question

I'm french, so, excuse my English.

What is the best method to take a string (char *) wrote on keyboard ?

Now, I use :

scanf("%256[^\n]", buffer);
while(getchar()!="\n")   /* to clear the buffer */
        {}

Thanks.
--



Sun, 19 Jan 2003 03:00:00 GMT  
 Question

in comp.lang.c.moderated:

Quote:
> I'm french, so, excuse my English.

> What is the best method to take a string (char *) wrote on keyboard ?

> Now, I use :

> scanf("%256[^\n]", buffer);
> while(getchar()!="\n")   /* to clear the buffer */
>         {}

> Thanks.

fgets(buffer, sizeof buffer, stdin);

Jack Klein
--
Home: http://jackklein.home.att.net
--



Sun, 19 Jan 2003 03:00:00 GMT  
 Question

Quote:

> I'm french, so, excuse my English.

I'm Dutch, so excuse my French <g>.

Quote:
> What is the best method to take a string (char *) wrote on keyboard ?

> Now, I use :

> scanf("%256[^\n]", buffer);
> while(getchar()!="\n")   /* to clear the buffer */
>         {}

Use fgets(). It gets a string, up to a maximum number of characters, or
until it hits '\n' or EOF. If it does hit '\n', it leaves it in the
string, so you can check whether you've read a complete line yet by
seeing if the current string has it.

Richard
--



Sun, 19 Jan 2003 03:00:00 GMT  
 Question

Quote:

> What is the best method to take a string (char *) wrote on keyboard ?

In computing, as in many other fields, such a thing as 'the' best
method to do something hardly ever exists.

Quote:
> Now, I use :
> scanf("%256[^\n]", buffer);
> while(getchar()!="\n")   /* to clear the buffer */
>         {}

As a rule of thumb, it's rarely a good idea to use 'scanf()'. It's
almost always preferrable to use fgets() on stdin, and sscanf() to
parse what was read.  Your use is one of the rare exceptions from that
rule, due to its cleverly set up format string.

The getchar() loop, however, has a bug: it doesn't check for EOF as
the return value, properly. If EOF is reached, this will be an endless
loop.

It's up to your application case whether ignoring any input beyond the
256th character of a line is acceptable.
--

Even if all the snow were burnt, ashes would remain.
--



Sun, 19 Jan 2003 03:00:00 GMT  
 Question

Quote:
> I'm french, so, excuse my English.

I'm Dutch, so excuse my English.  My French is inexcusable ;-)

Quote:
> What is the best method to take a string (char *) wrote on keyboard ?

Usually a single character at a time is the best way to do it. Then there
is no chance at all of buffer overflow. Also you will then be able to use
non-standard keys and key combinations.
It also means you'll need to use some things specific to the OS and
compiler you use and not covered by the ANSI C standard -- but so what.
Standards compliance is an ideal -- not something that can be reached in
most practical situations.

--
Greetings from
 _____
 /_|__| Auke Reitsma, Delft, The Netherlands.
/  | \  -------------------------------------
        Remove SPAMBLOCK from my address ...
--



Tue, 21 Jan 2003 03:00:00 GMT  
 Question

Quote:


>> What is the best method to take a string (char *) wrote on keyboard ?
> In computing, as in many other fields, such a thing as 'the' best
> method to do something hardly ever exists.
>> Now, I use :
>> scanf("%256[^\n]", buffer);
>> while(getchar()!="\n")   /* to clear the buffer */
>>         {}
> As a rule of thumb, it's rarely a good idea to use 'scanf()'. It's
> almost always preferrable to use fgets() on stdin, and sscanf() to
> parse what was read.  Your use is one of the rare exceptions from that
> rule, due to its cleverly set up format string.
> The getchar() loop, however, has a bug: it doesn't check for EOF as
> the return value, properly. If EOF is reached, this will be an endless
> loop.

It has another bug: it compares to "\n" (a pointer) instead of
'\n'. Use something like:

    while ((c = getchar()) != EOF && c != '\n') {   }

Regards,
Bernd

--
Bernd Eggink
Regionales Rechenzentrum der Uni Hamburg

http://www.rrz.uni-hamburg.de/eggink/BEggink.html
--



Tue, 21 Jan 2003 03:00:00 GMT  
 Question


Quote:


> >> What is the best method to take a string (char *) wrote on keyboard
?

> > In computing, as in many other fields, such a thing as 'the' best
> > method to do something hardly ever exists.

> >> Now, I use :

> >> scanf("%256[^\n]", buffer);
> >> while(getchar()!="\n")   /* to clear the buffer */
> >>         {}

> > As a rule of thumb, it's rarely a good idea to use 'scanf()'. It's
> > almost always preferrable to use fgets() on stdin, and sscanf() to
> > parse what was read.  Your use is one of the rare exceptions from
> > that rule, due to its cleverly set up format string.

> > The getchar() loop, however, has a bug: it doesn't check for EOF
> > as the return value, properly. If EOF is reached, this will be an
> > endless loop.

> It has another bug: it compares to "\n" (a pointer) instead of
> '\n'. Use something like:

>     while ((c = getchar()) != EOF && c != '\n') {   }

and, for the benefit of crusty types (such as myself) who absolutely
refuse to learn the umpty-ump precedence rules of C, add some parens:

    do {
      while ( ((c = getchar()) != EOF) && (c != '\n') ) {   }
      processthings();
    } while (c != EOF);

where the inner loop processes lines, and the outer handles files.  I
would find things much clearer if the scanf were omitted and the empty
block in the first while parsed the value.  No scanf call needed, and
the resultant code will not require the extensive library support.

This can be very significant in embedded systems.

--

 http://www.qwikpages.com/backstreets/cbfalconer/
--



Sat, 25 Jan 2003 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. question question question

2. This is a biztalk question but probably an easy question for you C# guru's

3. Non-Programming Question... experience question mostly

4. Not C program question, historical question

5. A question regarding FAQs question 12.2

6. Question FAQ question

7. Peer to peer board game - possible remoting question or design question

8. if(question) || if question

9. array question - beginner question

10. questions, questions, and than some

11. Solaris Unix Porting Question / Signals Question

12. A similar lint question (and questions about casts)

 

 
Powered by phpBB® Forum Software