Re: advice

From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 11/21/03


Date: Thu, 20 Nov 2003 23:19:25 GMT


"Jenski182" <jenski182@aol.comnojunk> wrote in message
news:20031120181038.01467.00000501@mb-m20.aol.com...
> ok this is my code :o)
> how can i stop it terminating after entering a string of more than 128
chars
> long? Thanks Jen x
[snip]

> //Get a string from stdin.
> gets (input);

[snip]

Never, never, never, *never* use gets(). Period.
See http://www.eskimo.com/~scs/C-faq/q12.23.html
(Read the whole FAQ, while you're at it, the returns
are well worth the effort imo.)

Use fgets(), which does feature a parameter with which
you can restrict the amount of input text stored.

Since it's outside control of the language, you'll still
need to deal with any possible 'extraneous' input that
might still reside in an OS's input buffer. Just use a
loop which extracts characters until a newline or EOF is
encountered.

-Mike



Relevant Pages

  • Re: var with only characters
    ... ending of the string. ... quantifier to match multiple chars. ... Modulo locale differences: ...
    (comp.lang.php)
  • Re: var with only characters
    ... ending of the string. ... quantifier to match multiple chars. ... Modulo locale differences: ...
    (comp.lang.php)
  • Re: Help needed Please!
    ... 60 int i=0; ... printf("You can end entering the text by using '#'\n"); ... If the string was not null-terminated that worked out of pure lack. ...
    (comp.lang.c)
  • Re: substring finding problem!
    ... banono going right to left without overlap ... <snip naming dropping rant> ... the string. ... something at which C programmers suck because C sucks at it. ...
    (comp.lang.c)
  • Re: Efficency and the standard library
    ... could represent this as a negative string length. ... Heathfield labors mightily to stay inside the Game: ... And so the Combatants turn once more to their game of reading C. ...
    (comp.lang.c)