afflush -- Flush File Buffers to Disk

SYNOPSIS

 #include <lcio.h>

 int afflush(FILE *f, int toeof);
 

DESCRIPTION

The afflush function flushes the output buffers for the FILE addressed by f to disk and performs additional system-dependent operations to ensure that the data will be accessible later, even if the program or the system fails. If the toeof argument is nonzero, the file is positioned at the end of the file as buffers are flushed; otherwise, the file position remains unchanged. You can use the afflush function with FILE opened for keyed access as well for text or binary access.

For files opened with the trunc=yes amparm specified or defaulted, afflush truncates the file at the current file position; that is, all characters after the current position are erased. If this behavior is not desired, a nonzero toeof argument can be specified to position to the end of file, thereby avoiding truncation.

For nondisk files, such as the terminal, afflush is treated the same as fflush, preceded by positioning at the end of the file if the toeof argument is nonzero.

The afflush function fails if the last file operation was a read. On completion of afflush, the next operation can be either a read or a write.

RETURN VALUES

The afflush function returns 0 or EOF if an error occurs.

CAUTION

For some file types, afflush may be rejected if the file is not positioned to the end of a record. This restriction applies to PDS members opened with grow=no.

Using afflush is significantly more costly than fflush.

IMPLEMENTATION

After flushing buffers, afflush ensures file integrity under MVS by issuing the CLOSE TYPE=T macro. Under CMS, it issues FSCLOSE for standard CMS disk files, followed by a call to FINIS for the associated minidisk, or it calls DMSCOMM (shared file commit) for CMS shared files. For OpenEdition HFS files, it invokes the fsync system call.

EXAMPLES

 #include <lcio.h>
  #include <stdlib.h>

  extern int num_updates;
  extern FILE *database;

  main()
  {
     int rc;
     extern int num_updates;
     extern FILE *database;
     int transaction(FILE *); /* Update database, return number of  */
                              /*  transactions or negative to quit. */

     database = fopen("dsn:userid.DATABASE", "r");

     num_updates = 0;         /* Reset update counter.              */
     for(;;) {     /* Run transactions until quitting time.         */
        int trans;
        trans = transaction(database);
           /* Perform the next transaction.                         */
        if (trans < 0) break;
        num_updates += trans; /* Monitor number of updates.         */
        if (num_updates >= 100) { /* Every 100 updates, checkpoint. */
            rc = afflush(database, 0);  /* Flush updates to disk.   */
            if (rc != 0) {
               puts("Error saving recent updates.");
               fclose(database);
               abort();
            }
        }
     }

     fclose(database);
  }

 

RELATED FUNCTIONS

fflush, fsync

SEE ALSO

I/O Functions

Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.