fflush() — Write buffer to file

Standards

Standards / Extensions C or C++ Dependencies

ISO C
POSIX.1
XPG4
XPG4.2
C99
Single UNIX Specification, Version 3
Language Environment

both  

Format

#include <stdio.h>

int fflush(FILE *stream);

#define _OPEN_SYS_UNLOCKED_EXT 1
#include <stdio.h>

int fflush_unlocked(FILE *stream);

General description

Flushes the stream pointed to by stream. If stream is NULL, it flushes all open streams.

The fflush() function is affected by the ungetc() and ungetwc() functions. Calling these functions causes fflush() to back up the file position when characters are pushed back. For details, see the ungetc() and ungetwc() functions respectively. If desired, the _EDC_COMPAT environment variable can be set at open time such that fflush() discards any pushed-back characters and leaves the file position where it was when the first ungetc() or ungetwc() function call was issued.

If fflush() is used after ungetwc() has pushed a wide char on a text stream, the position will be backed up by one wide character from the position the file was at when the ungetwc() was issued. For a wide-oriented binary stream, the position will be backed up based on the number of bytes used to represent the wide char in the ungetc buffer. For this reason, attempting to use ungetwc() on a character when the destination is a binary wide-oriented stream that was never read in the first place results in undefined behavior for fflush(). Note that the _EDC_COMPAT environment variable also changes the behavior of fflush() after ungetwc(), and will cause any wide char pushed-back to be discarded and the position left at the point where the ungetwc() was issued. For details on the _EDC_COMPAT environment variable, see the “Environment Variables” in z/OS XL C/C++ Programming Guide.

If fflush() fails, the position is left at the point in the file where the first ungetc() or ungetwc() function call was issued. All pushed-back characters are discarded.
Note: The system automatically flushes buffers when you close the stream or when a program ends normally without closing the stream.

The buffering mode and the file type can have an effect on when output data is flushed. For more information, see “Buffering of C Streams” in z/OS XL C/C++ Programming Guide.

stream remains open after the fflush() call. Because a read operation cannot immediately follow or precede a write operation, the fflush() function can be used to allow exchange between these two modes. The fflush() function can also be used to refresh the buffer when working with a reader and a simultaneous writer/updater.

fflush_unlocked() is functionally equivalent to fflush() with the exception that it is not thread-safe. This function can safely be used in a multithreaded application if and only if it is called while the invoking thread owns the (FILE*) object, as is the case after a successful call to either the flockfile() or ftrylockfile() function.

The fflush() function has no effect for files opened with type=blocked.

Returned value

If successful in flushing the buffer, fflush() returns 0.

If unsuccessful, fflush() returns EOF. When flushing all open files, a failure to flush any of the files causes EOF to be returned. However, flushing will continue on any other open files that can be flushed successfully.

Example

CELEBF15
⁄* CELEBF15                                      

   This example flushes a stream buffer.                                        
   It tests for the returned value of 0 to see if the flushing was              
   successful.                                                                  
                                                                                
 *⁄                                                                             
#include <stdio.h>                                                              
                                                                                
int retval;                                                                     
int main(void)                                                                  
{                                                                               
   FILE *stream;                                                                
   stream = fopen("myfile.dat", "w");                                           
                                                                                
   retval=fflush(stream);                                                       
   printf("return value=%i",retval);                                            
}                                                                               

Related information