file-handling · intermediate · ~15 min
fwrite a single fixed-size value.
Write a single int to a file as raw bytes, so it can be read back later.
Implement int write_int_binary(const char *path, int v) that writes the int v (native byte layout) to the file at path.
path: filename to write (created/overwritten in the working directory).v: the value to store.Return 0 on success, or -1 on failure (can't open or short write).
write_int_binary("wi_fixture.bin", 4242) -> 0
later: reading one int back from the file yields 4242
fwrite(&v, sizeof v, 1, f) must return 1; open with "wb"; close before returning.path: file to write. v: int value to store.
0 on success, -1 on failure.
fwrite one int (native layout) with "wb"; the write must return 1.
#include <stdio.h>
int write_int_binary(const char *path, int v) {
/* TODO */
return -1;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.