I am trying to scan in 1-3 words from the user into a string. However, Only the first word will scan.
-
Post the code of what you are doing.Mahesh– Mahesh2011-07-28 03:31:36 +00:00Commented Jul 28, 2011 at 3:31
-
Use search facility here. There are number of posts on scanf - stackoverflow.com/questions/tagged/scanfKV Prajapati– KV Prajapati2011-07-28 03:38:03 +00:00Commented Jul 28, 2011 at 3:38
-
Which company's scanner are you using ? ;)Arpit– Arpit2011-07-28 07:29:09 +00:00Commented Jul 28, 2011 at 7:29
Add a comment
|
2 Answers
scanf("%s", &area ) ;
scanf stops reading from the stream when a space is encountered. You need to use getline instead.
1 Comment
Nora Strong
scanf() is also a huge source of security holes. Not only is getline() more effective, it's also a lot safer.
One possibility is to use a scan set conversion instead of a string conversion:
char buffer[256];
scanf("%255[^\n]", buffer);
Much like fgets, this reads up to the end of the line rather than stopping at the first white-space character.
1 Comment
Jerry Coffin
@BillyONeal: Actually, scan-sets are part of standard C (e.g., §7.19.6.2/12 in C99, §7.9.6.2 in C89/90) -- definitely not the best known part, but there nonetheless.