file-handling · intermediate · ~15 min

File size via fseek/ftell

Measure a file with fseek + ftell.

Challenge

Implement long file_size(const char *path) returning the file's size in bytes (seek to the end, then ftell), or -1 if it can't be opened.

Starter code

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