Home > Archive > C > February 2006 > Re: do {...} while () ;
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Re: do {...} while () ;
|
|
| CBFalconer 2006-02-26, 3:55 am |
| Keith Thompson wrote:
> "Aleramo" <monferrato2006@yahoo.it> writes:
>
>
> Never use gets. It cannot be used safely.
>
> Never use silly abbreviations like "u" for "you"; they just make
> your text more difficult to read.
>
> Never post a followup via Google without first reading
> <http://cfaj.freeshell.org/google/>.
I wonder if he got the idea from this chorus of replies :-)
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
| |
| Marc Thrun 2006-02-26, 7:55 am |
| Aleramo wrote:
> Maybe i made a few of confusion. I addict some rows before:
>
> char scelta_array [10], scelta_piano ;
>
> puts ("Scrivere:\n* lineare, per studiare un array lineare;") ;
> puts ("* piano, per studiare un array piano;") ;
> puts ("* qualunque altra parola per uscire.") ;
> gets (scelta_array) ; /* Con scanf () non funziona!!! con gets
> dà il messaggio di worning ma non ga eco. */
>
> do {
> printf ("\nCome devono essere posizionati gli elementi?\n") ;
> printf ("* premere E, nel caso di posizionamento sul piano
> E\n") ;
> printf ("(ricezione onde orizzontali);\n") ;
> printf ("* premere H, nel caso di posizionamento sul piano
> H\n") ;
> printf ("(ricezione onde verticali).\n") ;
> scanf ("%c", &scelta_piano) ;
> } while ((scelta_piano != 'H') && (scelta_piano != 'h') &&
> (scelta_piano != 'E') && (scelta_piano != 'e')) ;
>
> Before i used:
>
> scanf ("%s", scelta_array) ;
>
> and i substituted it with:
>
> gets (scelta_array) ;
>
> I receive the message of Worning for gets (), but i can execute the
> program withouth any problems. My question is: have you thought i
> changet the scanf () in the cycle with the gets () ? it's the gets ()
> there ok? or you advise me to change the solution?
>
> Thank you to everyone!!!
> Aleramo.
>
Don't use gets() as it does not allow you to control the number of
characters to be read into the buffer. This leads to buffer overrun when
gets() tries to write beyond the supplied buffer. With fgets() you can
control the number of characters to be read and thus prevent buffer
overruns.
Also, have a look at http://c-faq.com/stdio/getsvsfgets.html as Eric
Sosman already stated.
| |
| Robin Haigh 2006-02-26, 6:55 pm |
|
"Aleramo" <monferrato2006@yahoo.it> wrote in message
news:1140948389.890846.190290@z34g2000cwc.googlegroups.com...
> Maybe i made a few of confusion. I addict some rows before:
>
> char scelta_array [10], scelta_piano ;
>
> puts ("Scrivere:\n* lineare, per studiare un array lineare;") ;
> puts ("* piano, per studiare un array piano;") ;
> puts ("* qualunque altra parola per uscire.") ;
> gets (scelta_array) ; /* Con scanf () non funziona!!! con gets
> dà il messaggio di worning ma non ga eco. */
>
> do {
> printf ("\nCome devono essere posizionati gli elementi?\n") ;
> printf ("* premere E, nel caso di posizionamento sul piano
> E\n") ;
> printf ("(ricezione onde orizzontali);\n") ;
> printf ("* premere H, nel caso di posizionamento sul piano
> H\n") ;
> printf ("(ricezione onde verticali).\n") ;
> scanf ("%c", &scelta_piano) ;
> } while ((scelta_piano != 'H') && (scelta_piano != 'h') &&
> (scelta_piano != 'E') && (scelta_piano != 'e')) ;
>
> Before i used:
>
> scanf ("%s", scelta_array) ;
>
> and i substituted it with:
>
> gets (scelta_array) ;
>
> I receive the message of Worning for gets (), but i can execute the
> program withouth any problems. My question is: have you thought i
> changet the scanf () in the cycle with the gets () ? it's the gets ()
> there ok? or you advise me to change the solution?
Your original line
scanf ("%s", scelta_array) ;
had two problems. One is that it doesn't discard the '\n' at end-of-line.
The other is the buffer overrun.
Using gets() is a fix for the '\n' problem. But it doesn't do anything
about the buffer overrun. It doesn't make it any worse than it was before,
it just gets the compiler and everybody else pointing it out.
Sadly, fgets() doesn't help much. Unlike gets(), it doesn't always read a
line. It avoids a buffer overrun at the risk of leaving surplus input in
the input stream, which is the problem you had to start with.
Incidentally, if you type an X instead of an H or an E, don't you get the
echo thing again?
--
RSH
| |
| Eric Sosman 2006-02-26, 6:55 pm |
| Robin Haigh wrote:
>
> Your original line
>
> scanf ("%s", scelta_array) ;
>
> had two problems. One is that it doesn't discard the '\n' at end-of-line.
> The other is the buffer overrun.
His original line was
scanf ("%c", &scelta_piano);
.... so it had only one of the two problems you mention.
--
Eric Sosman
esosman@acm-dot-org.invalid
|
|
|
|
|