Reads and writes binary files.
#include <stdio.h> size_t fread ( (void *) Pointer, Size, NumberOfItems, Stream) size_t Size, NumberOfItems; FILE *Stream; size_t fwrite (Pointer, Size, NumberOfItems, Stream) const void *Pointer; size_t Size, NumberOfItems; FILE *Stream;
The fread subroutine copies the number of data items specified by the NumberOfItems parameter from the input stream into an array beginning at the location pointed to by the Pointer parameter. Each data item has the form *Pointer.
The fread subroutine stops copying bytes if an end-of-file (EOF) or error condition is encountered while reading from the input specified by the Stream parameter, or when the number of data items specified by the NumberOfItems parameter have been copied. This subroutine leaves the file pointer of the Stream parameter, if defined, pointing to the byte following the last byte read. The fread subroutine does not change the contents of the Stream parameter.
The st_atime field will be marked for update by the first successful run of the fgetc, fgets, fgetwc, fgetws, fread, fscanf, getc, getchar, gets, or scanf subroutine using a stream that returns data not supplied by a prior call to the ungetc or ungetwc subroutine.
Note: The fread subroutine is a buffered read subroutine library call. It reads data in 4KB blocks. For tape block sizes greater than 4KB, use the open subroutine and read subroutine.
The fwrite subroutine writes items from the array pointed to by the Pointer parameter to the stream pointed to by the Stream parameter. Each item's size is specified by the Size parameter. The fwrite subroutine writes the number of items specified by the NumberOfItems parameter. The file-position indicator for the stream is advanced by the number of bytes successfully written. If an error occurs, the resulting value of the file-position indicator for the stream is indeterminate.
The fwrite subroutine appends items to the output stream from the array pointed to by the Pointer parameter. The fwrite subroutine appends as many items as specified in the NumberOfItems parameter.
The fwrite subroutine stops writing bytes if an error condition is encountered on the stream, or when the number of items of data specified by the NumberOfItems parameter have been written. The fwrite subroutine does not change the contents of the array pointed to by the Pointer parameter.
The st_ctime and st_mtime fields will be marked for update between the successful run of the fwrite subroutine and the next completion of a call to the fflush or fclose subroutine on the same stream, the next call to the exit subroutine, or the next call to the abort subroutine.
The fread and fwrite subroutines return the number of items actually transferred. If the NumberOfItems parameter contains a 0, no characters are transferred, and a value of 0 is returned. If the NumberOfItems parameter contains a negative number, it is translated to a positive number, since the NumberOfItems parameter is of the unsigned type.
If the fread subroutine is unsuccessful because the I/O stream is unbuffered or data needs to be read into the I/O stream's buffer, it returns one or more of the following error codes:
Note: Depending upon which library routine the application binds to, this subroutine may return EINTR. Refer to the signal subroutine regarding sa_restart.
If the fwrite subroutine is unsuccessful because the I/O stream is unbuffered or the I/O stream's buffer needs to be flushed, it returns one or more of the following error codes:
The fwrite subroutine is also unsuccessful due to the following error conditions:
ENOMEM | Indicates that insufficient storage space is available. |
ENXIO | Indicates that a request was made of a nonexistent device, or the request was outside the capabilities of the device. |
These subroutines are part of Base Operating System (BOS) Runtime.
The abort subroutine, exit subroutine, fflush or fclose subroutine, fopen, freopen, or fdopen subroutine, getc, getchar, fgetc, or getw subroutine, getwc, fgetwc, or getwchar subroutine, gets or fgets subroutine, getws or fgetws subroutine, open subroutine, print, fprintf, or sprintf subroutine, putc, putchar, fputc, or putw subroutine, putwc, putwchar, or fputwc subroutine, puts or fputs subroutine, putws or fputws subroutine, read subroutine, scanf, fscanf, sscanf, or wsscanf subroutine, ungetc or ungetwc subroutine, write subroutine.
The Input and Output Handling Programmer's Overview in AIX Version 4.3 General Programming Concepts: Writing and Debugging Programs.