file-handling · intermediate · ~15 min

Read the first N bytes

Use fread to pull raw bytes.

Challenge

Implement int read_first_bytes(const char *path, char *buf, int n) reading up to n bytes from the file into buf and returning the number actually read, or -1 if the file can't be opened.

Starter code

#include <stdio.h>

int read_first_bytes(const char *path, char *buf, int n) {
    /* TODO */
    return -1;
}

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