file-handling · intermediate · ~15 min

File size via stat

Query file metadata without opening the file.

Challenge

Implement long stat_size(const char *path) returning the file size using stat() (st_size), or -1 if stat fails.

Starter code

#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.