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
101
votes
7 answers

Using fflush(stdin)

So a quick Google search for fflush(stdin) for clearing the input buffer reveals numerous websites warning against using it. And yet that's exactly how my CS professor taught the class to do it. How bad is using fflush(stdin)? Should I really…
wrongusername
  • 18,564
  • 40
  • 130
  • 214
78
votes
1 answer

Flushing buffers in C

Should fflush() not be used to flush a buffer even if it is an output stream? What is it useful for? How do we flush a buffer in general?
mrk
  • 3,061
  • 1
  • 29
  • 34
72
votes
6 answers

Difference between fflush and fsync

I thought fsync() does fflush() internally, so using fsync() on a stream is OK. But I am getting an unexpected result when executed under network I/O. My code snippet: FILE* fp = fopen(file, "wb"); /* multiple fputs() calls like: */ fputs(buf,…
Adil
  • 2,418
  • 7
  • 34
  • 38
45
votes
5 answers

Can a program call fflush() on the same FILE* concurrently?

Can anything bad happen (like undefined behavior, file corruption, etc.) if several threads concurrently call fflush() on the same FILE* variable? Clarification: I don't mean writing the file concurrently. I only mean flushing it concurrently. The…
Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
24
votes
8 answers

I am not able to flush stdin. How can I flush stdin in C?

How to flush the stdin?? Why is it not working in the following code snippet? #include #include #include #include int main() { int i = 0, j = 0, sat; char arg[256]; char *argq; argq =…
Subodh Asthana
  • 293
  • 1
  • 2
  • 9
17
votes
3 answers

Understanding the need for fflush() and problems associated with it

Below is sample code for using fflush(): #include #include #include #include void flush(FILE *stream); int main(void) { FILE *stream; char msg[] = "This is a test"; /* create a file */ stream =…
karan
  • 8,637
  • 3
  • 41
  • 78
15
votes
2 answers

Why would I need use fflush on stdout before writing to stderr?

I am reading 'UNIX Network Programming: The Sockets Networking API' and in the example code they have an error handling function which contains the following lines: fflush(stdout); /* in case stdout and stderr are the same */ fputs(buf,…
dippynark
  • 2,743
  • 20
  • 58
11
votes
5 answers

Clear input buffer after fgets() in C

#include int main() { char name[10]; for(int i=0;i<=10;i++) { printf("Who are you? "); if(fgets(name,10,stdin)!=NULL) printf("Glad to meet you, %s.\n",name); } return(0); } When I input something greater than 10…
3lokh
  • 891
  • 4
  • 17
  • 39
11
votes
1 answer

When is FILE flushed?

I have a good old C FILE file descriptor under Windows that is used by an output stream to write data to. My question is simple and yet I could not find the answer: When is the content flushed to disc assuming I don't call fflush? The stream…
anhoppe
  • 4,287
  • 3
  • 46
  • 58
8
votes
4 answers

Extract unique IPs from live tcpdump capture

I am using the following command to output IPs from live tcpdump capture sudo tcpdump -nn -q ip -l | awk '{print $3; fflush(stdout)}' >> ips.txt I get the following output 192.168.0.100.50771 192.168.0.100.50770 192.168.0.100.50759 Need 2…
Carlos
  • 135
  • 1
  • 1
  • 8
8
votes
1 answer

In bash, How can I force a flush of an incomplete line printed to the terminal

I'm writing a script which does something like the following: echo -n "Doing stuff, wait for it... " do_stuff (($?==0)) && echo SUCCESS || echo FAILURE Excuse the poor bash skills. Anyway, the problem is that the first part of the line doesn't get…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
7
votes
1 answer

Can fseek(stdin,1,SEEK_SET) or rewind(stdin) be used to flush the input buffer instead of non-portable fflush(stdin)?

Since I discovered fflush(stdin) is not a portable way to deal with the familiar problem of "newline lurking in the input buffer",I have been using the following when I have to use scanf: while((c = getchar()) != '\n' && c != EOF); But today I…
Jugni
  • 255
  • 5
  • 12
6
votes
3 answers

fflush(stdin) function does not work

I can't seem to figure out what's wrong with this code: #include #include #include #include #define MAX 100 #define TRUE 1 #define FALSE 0 char sect_cat; char customer_name[MAX]; char…
6
votes
0 answers

windows console program stdout is buffered when using pipe redirection

i have a long run server program(say, program A) which is written in QT/c++. the program is not so stable so i decide to write a python script to restart it if it crashes. the problem is that the program may started fail(if i gave it an in-use…
adamhj
  • 155
  • 2
  • 8
6
votes
4 answers

Oracle PL/SQL UTL_FILE.PUT buffering

I'm writing a large file > 7MB from an Oracle stored procedure and the requirements are to have no line termination characters (no carriage return/line feed) at the end of each record. I've written a stored procedure using UTL_FILE.PUT and I'm…
Dustin
  • 101
  • 1
  • 1
  • 7
1
2 3
13 14