Skip to content
Justice Tech School

Binary Files

fread and fwrite move raw bytes. Open with "rb" and "wb". fseek and ftell move around inside the file.

Example
int out[3] = {10, 20, 30};
FILE *f = fopen("demo_c.bin", "wb");
fwrite(out, sizeof(int), 3, f);
fclose(f);
int in[3];
f = fopen("demo_c.bin", "rb");
fseek(f, sizeof(int), SEEK_SET);
fread(in, sizeof(int), 2, f);
printf("%d %d\n", in[0], in[1]);
fclose(f);
remove("demo_c.bin");

Result

20 30

Exercise

Write raw bytes.

(buf, 1, n, f)

Quiz Test

Which mode opens a binary file for reading?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.