You have several alternatives:
fflush(f);to flush the buffer to a specific point.setbuf(f, NULL);to disable buffering.
Where fis obviously yours FILE*.
t
#include <stdio.h>
int main(void)
{
char s[100];
FILE *f = fopen("test.txt", "w");
setbuf(f, NULL);
while (fgets(s, 100, stdin))
fputs(s, f);
return 0;
}
OR
#include <stdio.h>
int main(void)
{
char s[100];
FILE *f = fopen("test.txt", "w");
while (fgets(s, 100, stdin)) {
fputs(s, f);
fflush(f);
}
return 0;
}
source
share