file-handling · intermediate · ~15 min
Measure a file with fseek + ftell.
Measure a file's size in bytes by seeking to its end.
Implement long file_size(const char *path) that returns the size in bytes of the file at path, by seeking to the end and reading the position with ftell.
path: filename of a file the grader creates in the working directory.
Return the file size in bytes, or -1 if the file can't be opened.
file of 5 bytes -> 5
missing file -> -1
fseek to SEEK_END, then ftell.path: file to measure.
long size in bytes, or -1 if it can't be opened.
Seek to SEEK_END then ftell; open in binary mode.
#include <stdio.h>
long file_size(const char *path) {
/* TODO */
return -1;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.