[S5] Save struct into file

Estimated time: 25 minutes

You are currently processing an array composed of struct point defined below. In this programme, you need to store the content of the entire array in a file to be able to reuse it later. Write a C function to write the array composed of struct point into a file. The file may already exist or not. After the execution of the function, the file should only contain the array. If the file has to be created, the user who created it must have the permission to read it.

typedef struct point {
    int x;
    int y;
    int z;
} point_t;

Use only open(2), write(2) and close(2). You can only call write(2) once.

Hint : read carefully the man page of open(2) to manage all the cases mentionned above.

 * @pre pt != NULL, pointer to the first point_t in the array
 *      size > 0, the length of the array.
 *      filename != NULL
 * @post writes the array of point_t in the file.
 *       return 0 is everything worked correctly
 *       -1 if open() failed.
 *       -2 if write() failed.
 *       -3 if close() failed.
int save(point_t *pt, int size, char *filename) {


Author(s) Olivier Martin
Deadline No deadline
Submission limit No limitation
Category Tags S5, File


Open Close

Sign in