EntropySink
Technical & Scientific => Programming => Topic started by: ahluka on August 26, 2005, 10:22:35 AM
-
I've got a loop that outputs a prompt and reads in a 'command' into a buffer:
while (strcmp (cmd_in, "exit") != 0) {
printf (">> ");
scanf ("%s", cmd_in);
}
However, if I type something like 'hello world', the prompt is output twice. I placed a printf in there to output cmd_in 'in-between' the loops to see it's value, like so:
while (strcmp (cmd_in, "exit") != 0) {
printf (">> ");
scanf ("%s", cmd_in);
printf ("%s\n", cmd_in);
}
Now if I type a space (ala 'hello world') then I see this:
>> hello world
hello
>>
world
>>
So it seems scanf takes spaces as deliminaters for new input? It's really annoying now because I can't find any way to surpress it.
Any ideas?
-
Try using gets().
-
Thanks, worked a dream :)
-
gets and fscanf are unsafe because they don't check for the size of the input. Use fgets, which includes a parameter to limit the number of letters read
-
That's what you get taking advice for a guy that doesn't use C/C++ for a living (or in the past 2 years!)
-
Try using gets().
!! Ober you should know better! :p
-
Bah... I haven't touched C in close to 3 years... what do you expect ;)
-
fgets() for input and then you can sscanf() for parsing I guess.