[S5] Get and set on array stored in binary file

Estimated time: 30 minutes

Given a file containing a large array of integers, you have to write a function to edit the element at a given index the array and another function to retrieve a specific element from this array.

Since the array is huge, you cannot load it completely in memory. Consequently, you have to naviguate directly through the array in the file by using lseek(2).

You may want to use fstat(2) to obtain informations about a given file.

Use only open(2), read(2), write(2), close(2) and lseek(2).


Question 1: Retrieve the element at a given index
/*
 * @pre filename != NULL, index >= 0
 * @post return the integer at the index {index}
 *       of the array in the file {filename}.
 *       return -1 in case of error.
 *       return -2 if index >= length of array.
 */
int get(char *filename, int index) {
Question 2: Change the value at a given index
/*
 * @pre filename != NULL, index >= 0
 * @post set the element in the file {filename}
 *       at index {index} at value {value}.
 *       do nothing in case of errors
 */
void set(char *filename, int index, int value) {

Information

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

Tags

Close Open

Sign in