Your Browser is not longer supported

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

{{viewport.spaceProperty.prod}}

write - Write to a file (elementary)

&pagelevel(4)&pagelevel

Definition

#include <stdio.h>

int write(int fp, const char *buf, int n);

write is the elementary write operation.

write writes up to n contiguous bytes from the area to which buf points into the file with file descriptor fd.

SAM files are always processed as text files with elementary functions.

Parameters

int fd

File descriptor of the output file.

A file descriptor (positive integer) is the result of a successful open/open64 or creat/creat64 call.
File descriptors for stdin (0), stdout (1), and stderr (2) are assigned automatically when the program is started.

const char *buf

Pointer to the area containing the data to be written to the output file.

int n

Number of bytes to be written to the file. There is no guarantee that write will actually write n bytes!

Return val.

Number of bytes actually written



if successful.


-1

Nothing was written due to one of the following errors:

  • physical I/O error

  • fd is not a valid file descriptor

  • the file is not present

  • there is no access authorization or write permission for the file

  • the area in which the data is located was not correctly specified.

Notes

After each write call, you should check the number of bytes actually written.
If the result is smaller than the specification in n, there usually has been an error.
If the result is greater than the specification in n, tab characters (\t) were written to a text file.
In such cases, tab characters are converted to the corresponding blanks and counted in the number of bytes returned.

You should use the sizeof function to be sure that your specification in n does not exceed the size of the buffer.

The data is not written immediately to the external file but is 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)).

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 O_NOSPLIT specification was entered for open, records which are longer than the maximum record length are truncated to the maximum record length when they are written with write. By default (i.e. without the specification O_NOSPLIT), 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.

In the case of files with record-oriented input/output (record I/O), i.e. when the specification O_RECORD was entered for open, the write function writes a record to the file. In the case of SAM and PAM files the record is written to the current file position. In the case of ISAM files, the record is written to the position which corresponds to the key position in the record. If the number n of the characters to be written is greater than the maximum record length, only a record with the maximum record length is written. The remaining data is lost.
In the case of ISAM files a record is written only if it contains at least a complete key. If in the case of files with a fixed record length n is less than the record length, binary zeros are used for padding.


Example

The following program copies the standard input (file descriptor 0) to the standard output (file descriptor 1). If you utilize the redirection mechanism, you can use this program to copy from any source to any destination. BUFSIZ (8192 bytes) is defined in the include file <stdio.h>.

#include <stdio.h>
int main(void)
{
  char buf[BUFSIZ];
  int n;
  while((n = read(0, buf, sizeof(buf))) > 0)
        write(1, buf, n);
  return 0;
}

See also

read, open, open64, creat, creat64