Chapter Contents

Previous

Next
fwrite

fwrite



Write Items to a File

Portability: ISO/ANSI C conforming, UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
CAUTION
DIAGNOSTICS
PORTABILITY
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <stdio.h>

size_t fwrite(const void *ptr, size_t size,
              size_t count, FILE *f);


DESCRIPTION

fwrite writes one or more items of any type to the stream associated with the FILE object addressed by f . The size of each item is defined by size , count defines the number of items to be written, and ptr addresses the area containing the items to be written.

Although fwrite may be used to write characters, it is more frequently used to write noncharacter data, such as structured data. Except when fwrite is used to write printable character data, you should limit its use to binary streams because the library's transformation of control characters may change the data in unpredictable ways when reading and writing text streams.

Calls to fwrite to write items of type typeval commonly have the form

typeval buf[count];
fwrite(buf, sizeof(typeval), count, f);


RETURN VALUE

fwrite returns the number of items successfully written. It returns 0 if no items are written because of an error.


CAUTION

When using fwrite , remember that size is not necessarily a multiple of the record size, and that fwrite ignores record boundaries.


DIAGNOSTICS

If fwrite returns a value of 0, but count is greater than 0, an error occurred before any items were written.


PORTABILITY

Some implementations may return 0 in case of error, even though one or more items were successfully written.


IMPLEMENTATION

If count is less than 1, no output takes place. If an error occurs during the output operation, the file position is unpredictable.


EXAMPLE

The use of fwrite is illustrated in the example for fread.


RELATED FUNCTIONS

afwrite


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.