I’m using the “GCC C Compiler” as my compiler and I have a program that takes in inputs as stdin using “fgets” and then I’m using multiple printf’s to print results due to certain inputs.
However, my problem is I want the output to occur between the fgets, which they do reside in my code, however currently nothing prints until I return from main and the program ends.
Input Code:
int get_inputs(char** operands, char* delim) {
if (fgets(input,sizeof(input),stdin) == NULL) return 0; /* End of file */
/* Parse with StringParse, returns number of substrings */
return StringParse(input, operands, delim, 2, "+-*/^ ");
}
Output Code: (In a While(1) loop)
count = get_inputs(operands, delim);
switch(count) {
case 0:
printf("User Terminated\n");
return 0; /* User Terminated */
case 1: /* Single Value Input */
accumulator = atof(operands[0]);
printf("%g\n", accumulator);
break;
case 2:
if(strlen(operands[0]) == 0) { /* Operation First use Accumulator as input */
accumulator = doMath(accumulator, atof(operands[1]), delim[0]);
printf("%g\n", accumulator);
}
else { /* Two new values, replace Accumulator */
accumulator = doMath(atof(operands[0]), atof(operands[1]), delim[0]);
printf("%g\n", accumulator);
}
break;
default:
printf("Invalid Input\n"); /* Invalid Input or Error */
break;
}
Every other function is just doing math or string parsing.
Thanks in Advance!
This is because stdout is buffered to improve performance. The data is only pushed to the output pipe in larger chunks. To force this to happen at a certain point, add
In the code which is writing to stdout.