fwrite
| Input-Output Functions |
Write unformatted data from a buffer to a stream.
size_t fwrite( const void *buffer, size_t size, size_t count, FILE *file_pointer);
| buffer | The buffer containing the data. |
| size | The size of the buffer. |
| count | The number of bytes to store in the buffer. |
| file_pointer | A pointer to a file that will store the data. |
Return Values
If successful, the function returns the number of full items (not bytes) successfully written.
Example
The following example gives values to a student record, sd, and then writes it to a file using fwrite.
#include <stdio.h>
#ifdef unix char * filename = "/tmp/marks.txt"; #else char * filename = "c:\\marks.txt"; #endif
struct student_data {
int student_id;
unsigned char marks[10];
};FILE* file; struct student_data sd; int i;
if ((file = fopen(filename, "w+")) == NULL) {
lr_output_message ("Unable to create %s", filename);
return -1;
}sd.student_id = 1001;
// Set the marks of student 1001
for (i = 0; i < 10; i++)
sd.marks[i] = (unsigned char)(85 + i);// Write student record to the file
i = fwrite(&sd, sizeof(struct student_data), 1, file);
if (i > 0)
lr_output_message ("Successfully wrote %d record", i);fclose(file);
Example: Output:
Successfully wrote 1 record

