Questions tagged [fflush]

The C standard library `fflush` is used to synchronize the stream on which it is invoked with the actual content of the corresponding file. It can be used only on output streams. A similar function is defined in C++ as `std::fflush`.

The C standard library function fflush function is defined in the <stdio.h> header and its documentation can be found here.

The corresponding C++ standard library function std::fflush is defined in <cstdio> header and documented here.

197 questions
1
vote
0 answers

What is the use of fflush(stdout) in C and how to use it?

I searched the web for this, but can't find satisfactory result. Can anyone show me the use of fflush(stdout) in C and a simple code that can be run with and without fflush(stdout) so that it is easy to figure out the difference in the output. Also…
1
vote
2 answers

Is it safe to call fflush() without fclose()

I am writing logger for my embedded application. I need to write all logs to file. Currently I am opening and closing file for every write. To improve performance, is it safe to keep log file open throughout the application scope and call fflush()…
1
vote
1 answer

popen()ed pipe closed from other extreme kills my program

I have a pipe which I opened with FILE *telnet = popen("telnet server", "w". If telnet exits after a while because server is not found, the pipe is closed from the other extreme. Then I would expect some error, either in fprintf(telnet, ...) or…
1
vote
1 answer

No output over SSH before waiting for input via scanf()

I have a file called get_int.c on a remote Unix system, containing the following: #include int main() { int input; printf("Give an integer: "); fflush(stdout); scanf("%d", &input); printf("Try again: "); …
jhud
  • 49
  • 2
  • 9
1
vote
0 answers

How to use fflush() when multiple processes print to same stdout

I am creating multiple child processes and they all print to same STDOUT file, even when they have to wait for a mutex to print and even fflush(stdout), the printing still seems to overlap one another. I have added mutexes to surround every printf,…
Alex Marasco
  • 157
  • 3
  • 10
1
vote
1 answer

How to read characters from stdin into an array in c?

I last did C in 1991 and now I'm helping a friend with homework. He has to get characters from stdin into an array. Seems simple enough. I figured I'd use this question as a reference point. We have this: printf("Input the line\n"); …
Ole
  • 41,793
  • 59
  • 191
  • 359
1
vote
2 answers

fflush fails on Visual C++ 2010

I'm trying to run some code but fflush() with the error: Invalid file descriptor. File possibly closed by a different thread Here is the relevant part of the code: fhandle = fopen("dbfile.bin", "rbc"); /* There is a valid dbfile.bin file -…
chustar
  • 12,225
  • 24
  • 81
  • 119
1
vote
1 answer

Array is not reseting correctly, when using memset

When I take a vale from stdin that is to large for c the reset is not behaving as expected. The user will be re-prompted to input the code but after inputing an additional new line is presented where input is needed, than the length check is…
1
vote
1 answer

Buffer flushing not working for printf function

Coming from here: https://stackoverflow.com/a/1716621/1461017 I want to print separate points (dots, "."), one at a time on the same line, under control of a for loop: for (i=0; i<100; i++) { printf("."); sleep(1); } printf("\n"); But the…
Sopalajo de Arrierez
  • 3,543
  • 4
  • 34
  • 52
1
vote
1 answer

Printf function in C not printing until while loop finished

I want to test if different loops are active, so I have a print statement that repeats every 500ms in each loop, however the print statement does not print every 500ms, it waits until the loop is finished and then prints everything at once instead…
1
vote
1 answer

Clearing the buffer when using Getchar (there must be a better way!)

I am writing a function where I only need to obtain a single digit, I decided to use getchar() instead of scanf(). However since I only need one character I did not use an array to obtain it. This causes a problem because I dont have a loop to find…
DorianD
  • 11
  • 1
1
vote
4 answers

fflush and while loop

I have been trying to use fflush to make a progress bar. To test fflush, I wrote the small code below. It works as it supposed to when I uncomment "sleep(1);" but it works in an unexpected way if it remains commented out. It prints the first…
yam
  • 1,383
  • 3
  • 15
  • 34
1
vote
3 answers

How does fflush work?

I'm not sure if I properly understand how flushing works in C. I just can't get it to work as described in multiple manuals and reference books. Here's an example with comments: #include int main(void) { int x; char ch; …
user4087080
1
vote
2 answers

modify fflush() that guarantee calling ungetc() twice in a row in C

I'm a C beginner, I want to call ungetc() twice in a row although I know in regular C it is not permitted. Someone told me I can modify Fflush() to do this job, however I don't know how to do it. Here is my code, my Fflush only allow one ungetc(), I…
yuan wang
  • 27
  • 4
1
vote
1 answer

Keeping 'almost complete' logs even when system crashes

We have a c++ application (console) that runs on windows and unix. This application used output files to output verbose log files of system calls/prints/etc. The prblem is, that in certain occasion we might get signal 11/2 after new features are…
Moshe
  • 11
  • 1