Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

fwrite - Write blockwise to a file

&pagelevel(4)&pagelevel

Definition   

#include <stdio.h>

size_t fwrite(const void *p, size_t elsize, size_t n, FILE *fp);

fwrite writes n elements (elsize bytes in size each) from the area pointed to by p to the file with file pointer fp.
The position of the read/write pointer is subsequently advanced by the number of bytes written.

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 sizeof function for the size of a data unit to which p points.

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

fwrite writes a record to the file.

For sequential files (SAM, PAM), the record is written at the current file position.
For indexed-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 = element length * number of elements

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.

fwrite produces the same return value as for stream I/O, namely the number of elements written in their entirety. For record I/O it is best to use only element length 1 since in this case the return value corresponds 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.

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.

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