__fpurge() — Discard pending data in a stream

Standards

Standards / Extensions C or C++ Dependencies
Language Environment both None

Format

#include <stdio.h>
#include <stdio_ext.h>

void __fpurge(FILE *stream);

#define _OPEN_SYS_UNLOCKEKD 1
#include <stdio.h>
#include <stdio_ext.h>

void __fpurge_unlocked(FILE *stream);

General description

The _fpurge() function requests that any pending data in the stream be discarded.

After a call to __fpurge(), if stream is currently reading, any data that has been read from the system but not yet presented to the application will be discarded. Similarly, any data that has been pushed back into the steam with ungetc() will also be discarded.

After a call to __fpurge(), if stream is currently writing, any data that the application has requested to be written, but has not yet been flushed to an output device will be discarded. Any data that is written to the buffer after a call to __fpurge() is destined to be written out, to the block where the discarded data was originally intended to be placed, either explicitly via fflush() or implicity once the buffer is full.

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

Usage note

The __fpurge() function is allowed to be called on UNIX files only. Calling this function on any other type of file will not cause pending data in the buffer to be discarded. The function will return and result in errno being set to a nonzero value.

Returned value

The _fpurge() function returns no values.

An application wishing to check for error situations should set errno to 0, then call __fpurge(), and then check errno. If errno is nonzero, assume that an error has occurred.

Error Code
Description
EBADF
The stream specified by stream is not valid.

Example

CELEBF86
/* CELEBF86

   This example purges pending data in the I/O buffer.

*/

#include <stdio.h>
#include <errno.h>
#include <stdio_ext.h>

void main(int argc,char** argv){
   char *bufflush="data will be written into file";
   char *bufpurge="data will be not written into file";
   FILE *fp=fopen("./myfile","a");
   if(NULL==fp){
      perror("failed to open file\n");
      exit(0);
   }
   fwrite(bufflush,strlen(bufflush),1,fp);
   fflush(fp);
   fwrite(bufpurge,strlen(bufpurge),1,fp);
   errno=0;
   __fpurge(fp);
   if(errno!=0)
      perror("call __fpurge() failed\n");
   fclose(fp);
}
Output
The file "myfile" contains the text - data will be written into file

Related information