[英]Don't print the last character: print only after the next input
我有一個問題,當我嘗試打印輸入時,程序不會打印最后一個字符串(在本例中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
是完全有效的,但它也有打印換行符的副作用,還有其他選項,即:
在printf
之后使用fflush(stdout)
將刷新緩沖區,你不需要\n
。
您可以將緩沖模式更改為無緩沖,每個 output 都會盡快寫入。 同樣,不需要\n
。
setvbuf(stdout, NULL, _IONBF, 0);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.