file-handling · intermediate · ~15 min

Read a whole file as text

`fseek`/`ftell` to size a file, then a single `fread`.

Challenge

Implement char *read_all(const char *path, size_t *out_len). Allocate a buffer with the file's contents plus a trailing NUL byte (so the result is a valid C string for text files). Return NULL on failure. The caller frees.

Starter code

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>

char *read_all(const char *path, size_t *out_len) {
    /* TODO */
    return NULL;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.