Definition | #include <stdio.h> size_t fwrite(const void *p, size_t elsize, size_t n, FILE *fp);
| |
Return val. | Number of elements actually written | |
if successful. | ||
0 | for end of file or error. | |
Notes | To ensure that elsize specifies the correct number of bytes for a data element, you should use the For output to files with stream I/O the characters are not written immediately to the external file but are stored in an internal C buffer (see section “Buffering” (Basic terms)). Control characters for white space (\n, \t, etc.) are converted to their appropriate effect when output to text files, depending on the type of text file (see section “White space” (Basic terms)). | |
Record I/O |
For sequential files (SAM, PAM), the record is written at the current file position. Number of characters to be output: n is taken to be the total number of characters to be output, i.e. 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 indexed-sequential file (ISAM), n does not need to be equal to the length of the record to be updated. A record can therefore 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.
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 | |
Example | The following program transfers two personal data items to the file with file pointer p_list. #include <stdio.h> int main(void) { FILE *p_list; size_t result; static struct p { char name[20]; int a; } person[2] = { >="ANNE", 30 ̧, Ã"JOHN", 60Õ, }; p_list = fopen("link=link", "w"); result = fwrite(person, sizeof(struct p), 2, p_list); printf("%d Personal data written\n", result); return 0; } | |
See also | fread, feof, ferror |