[S5] Reading integers in a binary file

Estimated time: 25 minutes

Given a binary file containing some (possibly none) positive integers (int) that were stored in the file by successive calls of write(fd,&num,sizeof(int)) write a code that computes the sum of all integers that were stored in the file. The function returns the sum when there are no errors. In case of errors, it returns.

  • If open() fails, return -1.
  • If read() fails, return -2.
  • If close() fails, return -3.

You can only use open(2), read(2), write(2) and close(2).


Write your code here
/*
 * @pre filename != NULL, name of the file
 * @post returns the sum of all integers stored in the binary file.
 *       return -1 if the file can not be open.
 *       return -2 if an error occurs during reading
 *       return -3 if close() fails.
 */
int sum_file(char *filename) {

Information

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

Tags

Close Open

Sign in