file-handling · beginner · ~10 min
Use append mode to add to a file without truncating it.
int append_line(const char *path, const char *line);
Open path in append mode, write line followed by a newline, close. Return
the number of bytes written (strlen(line) + 1), or -1 on NULL args or open
failure.
fopen(path, "a").fprintf(f, "%s\n", line) returns the count written.Append mode is how logs and audit trails grow without clobbering existing content.
#include <stdio.h>
int append_line(const char *path, const char *line) {
/* TODO */
(void)path; (void)line;
return -1;
}
Using "w" (truncates the file). Forgetting the newline. Not closing on failure.
First write creates the file. NULL path or line → -1.
O(len).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.