##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# fwrite

From cppreference.com
< c‎ | io

Defined in header `<stdio.h>` |  |

size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream ); |  | (until C99)
size_t fwrite( const void *restrict buffer, size_t size, size_t count, FILE *restrict stream ); |  | (since C99)

Writes `count` of objects in the given array `buffer` to the output stream `stream`. Objects are not interpreted in any way.

### [edit] Parameters

buffer | - | pointer to the first object object in the array to be written
size | - | size of each object
count | - | the number of the objects to be written

### [edit] Return value

The number of objects written successfully.

### [edit] Example

Run this code

    #include <stdio.h>

    int main()
    {
        // write buffer to file
        FILE *f1;
        char buffer[] = { 'x' , 'y' , 'z' };
        f1 = fopen("file.bin", "wb");
        fwrite(buffer, sizeof(char), sizeof(buffer), f1);
        fclose(f1);

        // read the same data and print it to the standard output
        FILE *f2;
        char rbuf[10];
        f2 = fopen("file.bin", "rb");
        const char* res = fgets(rbuf, sizeof(rbuf), f2);
        fclose(f2);

        if (res) { // points to rbuf on read success, NULL on failure
            puts(res);
        }
    }

Output:

    xyz

### [edit] See also

printf fprintf sprintf snprintf (C99) | prints formatted output to stdout, a file stream or a buffer (function) [edit]
fputs | writes a character string to a file stream (function) [edit]
fread | reads from a file (function) [edit]
C++ documentation for fwrite

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=c/io/fwrite&oldid=66065"