【发布时间】:2021-07-03 03:35:33
【问题描述】:
我有一个问题,当我尝试打印输入时,程序没有打印最后一个字符串(在本例中为 var_quantita)。
但是如果我添加一个\n,或者如果我从stdin 发送另一个命令,它就会起作用。
所以我认为问题与最后一个字符串有关,但我不确定。
我的代码:
uint32_t var_quantita;
uint8_t var_tipo[BUF_SIZE];
//...
memset(com_par, 0, BUF_SIZE);
memset(comando, 0, BUF_SIZE);
memset(arg2, 0, BUF_SIZE);
memset(arg3, 0, BUF_SIZE);
memset(arg4, 0, BUF_SIZE);
//prendo in ingresso il comando e i parametri
fgets(com_par, BUF_SIZE, stdin);
sscanf(com_par, "%s %s %s %s", comando, arg2, arg3, arg4);
printf("Argomenti inviati:%s %s %s %s \n", comando, arg2, arg3, arg4);
//......
if(strcmp(comando, "add\0") == 0){
strcpy(var_tipo, arg2);
var_quantita = atoi(arg3);
printf("Tipo:%s\nQuantita:%d", var_tipo, var_quantita);
}//fine if(add)
【问题讨论】:
-
到终端的输出通常是缓冲的。发送
\n会刷新输出缓冲区。试试这个,或者fflush(stdout); -
是的,使用 {\n} 可以,但我不知道:模式是否正确?还是想绕过问题?
-
fflush 是正确的方法
-
好的,谢谢两位!