file-handling · intermediate · ~15 min
`fseek`/`ftell` to size a file, then a single `fread`.
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.
#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.