file-handling · intermediate · ~15 min

Count lines in a file

Iterate a text file line by line with fgets.

Challenge

Implement int count_file_lines(const char *path) returning the number of lines (use fgets), or -1 if the file can't be opened.

Starter code

#include <stdio.h>

int count_file_lines(const char *path) {
    /* TODO */
    return -1;
}

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