Name

fflush - flushes a stream

Library

libc.lib

Synopsis

  #include <stdio.h>
  int fflush (FILE *stream);

Return values

Upon successful completion 0 is returned. Otherwise, EOF is returned and the global variable errno is set to indicate the error.

Detailed description

The function fflush forces a write of all buffered data for the given output or update stream via the stream’s underlying write function. The open status of the stream is unaffected.

If the stream argument is NULL, fflush flushes all open output streams.


Examples

/****************** this program shows flushing user space buffered data using fflush **************/
#include <stdio.h>
int main()
{
        FILE *fp;
        int retval = 0;
        char name[20] = "c:\\flush1.txt";
        fp = fopen(name, "w+"); 
        if(fp == NULL)
                {               
                printf("Error : File open");
                return -1;
                }
        setvbuf(fp, NULL, _IOFBF, 100);  // set to full buffering with NULL buffer      
        fprintf(fp, "we are trying to buffer 100 characters at once with NULL buffer.");
        
        retval = fflush(fp);
        if (retval)
                {               
                printf("fflush failed\n");
                fclose(fp);
                unlink(name);
                return -1;
                }
        else printf("Buffer successfully flushed\n");
        fclose(fp);
        unlink(name);
        return 0;
}

         

Output

we are trying to buffer 100 characters at once with NULL buffer.
Buffer successfully flushed

         

Errors

[EBADF]
  The stream argument is not an open stream, or, in the case of fflush, not a stream open for writing.

The function fflush may also fail and set errno for any of the errors specified for the routine write.


See also

write, fclose, fopen, setbuf

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top