_flushlbf() — Flush all open line-buffered files

Standards

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

Format

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

void _flushlbf(void);

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

void _flushlbf_unlocked(void);

General description

The _flushlbf() function flushes all open line-buffered streams.

The _flushlbf() function is affected by the ungetc() and ungetwc() functions. Calling this function causes _flushlbf() to back up the file position when characters are pushed back. For details, see ungetc() — Push character onto input stream and ungetwc() — Push a wide character onto a stream. If needed, the _EDC_COMPAT environment variable can be set at open time such that _flushlbf() discards any pushed-back characters and leaves the file position where it was when the first ungetc() or ungetwc() function call was issued.

If the _flushlbf() function is used after the ungetwc() function pushed a wide char on a text stream, the position will be backed up by one wide character from the position of the file when the ungetwc() function was issued. For a wide-oriented binary stream, the position will be backed up based on the number of bytes that are 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 _flushlbf(). Note that the _EDC_COMPAT environment variable also changes the behavior of _flushlbf() 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 about the _EDC_COMPAT environment variable, see Environment Variables in z/OS XL C/C++ Programming Guide.

If _flushlbf() 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:
  1. The system automatically flushes buffers when you close the stream or when a program ends normally without closing the stream.
  2. The _flushlbf() function has no effect on line-buffered text files, because z/OS® XL C/C++ writes all records to the system as they are completed. All incomplete new records remain in the buffer.

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.

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

The _flushlbf_unlocked() function is equivalent to the _flushlbf() function with the exception that it is not thread-safe. This function can be safely used in a multithreaded application whether the user has locked all open line-buffered files or not.

When flushing all open line-buffered files, a failure to flush any of the files will leave it unchanged. However, flushing will continue on any other open line-buffered files that can be flushed successfully.

Returned value

The _flushlbf() function returns no values.

Example

CELEBF84
/* CELEBF84

   This example flushes all the line-buffered files.
   
*/ 

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

#define BUF_SIZE 128

int main(void)
{
   char lbuf[BUF_SIZE]; /* line buffer */
   char fbuf[BUF_SIZE]; /* full buffer */
   char *tagstr = "This file was modified!";
   FILE *lfp;
   FILE *ffp;

   lfp = fopen("newlfile.dat", "a+");
   if(lfp == NULL){
      perror("Open file failed!\n");
      return -1;
   }
   if(setvbuf(lfp, lbuf, _IOLBF, sizeof(lbuf)) != 0){ /* set lbuf to line-buffered */
      perror("Format line-buffered failed!\n");
      fclose(lfp);
      return -1;
   }

   if (__flbf(lfp)) printf("newlfile.dat is line-buffered\n");
   else printf("newlfile.dat is not line-buffered\n");

   if(fwrite(lfp,strlen(tagstr), 1, lfp) != 1){ /* write tag string to line buffer*/
      perror("Write line buffer failed!\n");
      fclose(lfp);
      return -1;
   }
   printf("Write to the line buffered file succeeded\n");

   ffp = fopen("newffile.dat", "a+");
   if(ffp == NULL){
      perror("Open file failed!\n");
      fclose(lfp);
      return -1;
   }
   if(setvbuf(ffp, fbuf, _IOFBF, sizeof(fbuf)) != 0){ /* set fbuf to full-buffered */
      perror("Format full-buffered failed!\n");
      fclose(ffp);
      return -1;
   }

   if (__flbf(ffp)) printf("newffile.dat is line-buffered\n");
   else printf("newffile.dat is not line-buffered\n");

   if(fwrite(tagstr, strlen(tagstr), 1, ffp) != 1){ /* write tag string to full buffer */
      perror("Write full buffer failed!\n");
      fclose(lfp);
      fclose(ffp);
      return -1;
   }
   printf("Write to the full buffered file succeeded\n");
   _flushlbf(); /* flush line buffered files */
   printf("Only line buffered files are flushed...\n");
   fclose(lfp);
   fclose(ffp);
   return 0;
}
Output
newlfile.dat is line-buffered
Write to the line buffered file succeeded
newffile.dat is not line-buffered
Write to the full buffered file succeeded
Only line buffered files are flushed...

Related information