file-handling · intermediate · ~15 min

Write lines to file

Sequential output with `fopen`/`fputs`/`fclose`.

Challenge

Implement int write_lines(const char *path, const char *const *lines, size_t n) that writes each string from lines to path, one per line (each terminated by \n). Return 0 on success and -1 on failure (e.g., fopen returns NULL).

Starter code

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

int write_lines(const char *path, const char *const *lines, size_t n) {
    /* TODO */
    return -1;
}

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