Share this page

# Example: fwrite

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

Tips to improve your search results... VuGen Function Reference Version 25.1-25.3 Function Reference 25.1-25.3 All View Local Help Center View Online Help Center Version: