Description | fwrite() writes, from the array pointed to by ptr, up to nitems elements whose size is specified by size, to the stream pointed to by stream. The file-position indicator for the stream (if defined) 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 structure components st_ctime and st_mtime of the file are marked for changing between successful execution of fwrite() and the next successful completion of a call to fflush() or fclose() for the same data stream or a call to exit() or abort() (see sys/stat.h ). BS2000 Record I/O fwrite() writes a record to the file.
For sequential files (SAM, PAM), the record is written at the current file position . For index-sequential files (ISAM), the record is written at the position corresponding to the key value in the record. Number of characters to be output: n is taken to be the total number of characters to be output, i.e. n = size * nitems If n is greater than the maximum record length, only one record with the maximum record length is written. The remaining data is lost. If n is less than the minimum record length no record is written. The minimum record length is defined only for ISAM files and means that n must cover at least the area of the key in the record. If n is less than the record length when a record is written to a file with fixed record length, the record is padded with binary zeros at the end. When an existing record is updated in a sequential file (SAM, PAM), n must be equal to the length of the record to be updated. Otherwise, an error occurs. In PAM files, the record length is the length of a logical block. When an existing record is updated in an index-sequential file (ISAM), n need not be equal to the length of the record to be updated. In other words, a record can be shortened or lengthened.
In ISAM files for which key duplication is permitted, it is not possible to perform a direct update on a record. Whenever a record with an existing key is written, a new record is written. The old record must be explicitly deleted. fwrite() produces the same return value as for stream I/O, i.e. the number of elements written in their entirety. For record I/O, it is best to use only an element length of 1, since the return value will then correspond to the length of the record written (without any record length field). In the case of a fixed record length, however, any required padding with binary zeros is not taken into account in the return value. (End)
|
Notes | To ensure that size specifies the correct number of bytes for a data element, the sizeof() function should be used for the size of the data unit to which ptr points. On output to files with stream I/O, data is not written immediately to the external file, but is stored in an internal C buffer (see section Buffering streams” ). On output to text files, control characters for white space (\n , \t , etc.) are converted to their appropriate effect in accordance with the type of text file (see section “White-spacecharacters” ). BS2000 The following applies in the case of text files with SAM access mode and variable record length for which a maximum record length is also specified: When the specification split=no was entered for fopen() , records which are longer than the maximum record length are truncated to the maximum record length when they are written. By default or with the specification split=yes , these records are split into multiple records. If a record has precisely the maximum record length, a record of the length zero is written after it. (End) The program environment determines whether fwrite() is executed for a BS2000 or POSIX file.. |
See also | ferror() , fopen() , printf() , putc() , puts() , write() , stdio.h , sys/stat.h .
|