通过scanf抑制输入中的空白区域

时间:2009-04-23 15:11:20

标签: c stdio

要抑制换行符,我们使用%[^ \ n]。你能否建议在输入中跳过空白的格式应该是什么,即如果我们必须输入“hallo stackflow”。我知道fgets和gets但是我不想使用它们,它们正在制造问题。

2 个答案:

答案 0 :(得分:1)

我认为你的意思是“包括空白”。使用:

#define str(x) #x
#define xstr(x) str(x)
/* ... */
char buf[ SIZE + 1 ] = "";
int rc = scanf("%" xstr(SIZE) "[^\n]%*[^\n]", buf);
/*you may need the return value later on, if reading
  in multiple strings with whitespaces in a loop */
if (!feof(stdin)) 
     getchar(); /* consume newline */

答案 1 :(得分:0)

scanf格式填空;这会抑制空白。