Thông tin

Tác giả Olivier Martin, Cyril Pletinckx, Minh-Phuong Tran
Hạn chót Không có hạn chót
Giới hạn nộp bài Không có giới hạn
Các tag chuyên mục s5, level4, category_mmap, category_file

Tags

Đăng nhập

[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 in the array and another function to retrieve a specific element from this array.

Use only open(2), mmap(2), munmap(2), msync(2), fstat(2) and close(2).

Hint : msync(2) is a function that ensures that your modifications done in memory (so in the address returned by mmap) will also be saved in the file. Check the documentation to learn how to use it (pay attention to the flags). Call it before munmap() (or your modifications to the memory may be lost) !


Câu hỏi 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) {
Câu hỏi 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) {