file-handling · intermediate · ~15 min

Read the first line

Read one line and strip the newline.

Challenge

Implement int first_line(const char *path, char *out, int outsz) copying the first line (without the trailing newline) into out and returning its length, or -1 if the file can't be opened.

Starter code

#include <stdio.h>
#include <string.h>

int first_line(const char *path, char *out, int outsz) {
    /* TODO */
    return -1;
}

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