file-handling · intermediate · ~15 min

Write bytes to a file

Use fwrite to persist raw bytes.

Challenge

Implement int write_bytes(const char *path, const char *data, int n) writing n bytes from data to path (truncating), returning the number written, or -1 if it can't be opened.

Starter code

#include <stdio.h>

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

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