file-handling · intermediate · ~15 min
Query file metadata without opening the file.
Get a file's size from its metadata, without opening the file.
Implement long stat_size(const char *path) that returns the size in bytes of the file at path, using stat() and its st_size field.
path: filename of a file the grader creates in the working directory.
Return the file size in bytes, or -1 if stat fails (e.g. the file doesn't exist).
file of 5 bytes -> 5
missing file -> -1
struct stat + stat(path, &st); read st.st_size. Do not open the file.path: file to measure.
long size from st_size, or -1 if stat fails.
Use stat(); do not open the file.
#include <sys/stat.h>
long stat_size(const char *path) {
/* TODO */
return -1;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.