How do I scan spaces into a a string?

2019-02-25 16:28发布

I am trying to scan in 1-3 words from the user into a string. However, Only the first word will scan.

标签: c scanf
2条回答
Juvenile、少年°
2楼-- · 2019-02-25 16:33
scanf("%s", &area ) ;

scanf stops reading from the stream when a space is encountered. You need to use getline instead.

查看更多
时光不老,我们不散
3楼-- · 2019-02-25 16:35

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.

查看更多
登录 后发表回答