Reputation: 41
I have a problem, when I try to print an input, the program doesn't print the last string (in this case var_quantita
).
But if I add an \n
, or if I send another command from stdin
, it works.
So I think that the problem is related to the last string, but I'm not sure.
My code:
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)
Upvotes: 1
Views: 181
Reputation: 23802
The buffering of your system is set to be line buffered, characters are transmitted from the buffer as a block when a new-line character is encountered. Using \n
is perfectly valid, but it has the side effect of also printing a newline, there are other options, namely:
Using fflush(stdout)
after the printf
will flush the buffer regarless, you won't need \n
.
You can change your buffering mode to have no buffering, each output is written as soon as possible. Again, no \n
will be needed.
setvbuf(stdout, NULL, _IONBF, 0);
Upvotes: 1