How can I read a string with spaces in it in C?

Johny picture Johny · Oct 26, 2010 · Viewed 28.1k times · Source

scanf("%s",str) won't do it. It will stop reading at the first space. gets(str) doesn't work either when the string is large. Any ideas?

Answer

NG. picture NG. · Oct 26, 2010

use fgets with STDIN as the file stream. Then you can specify the amount of data you want to read and where to put it.