Binary I/O stores bytes exactly as they appear in memory for a chosen value.

write count `fwrite` reports how many items were written.
round trip Reading the value back confirms the same item was stored.

Binary Write

value
binary_write.c
Replay: real traced execution (multi-file project)
#include <stdio.h>

int main(void) {
    int value = 7;
    int readBack = 0;
    FILE *out = fopen("egtry_c17_binary_write.bin", "wb");
    int wrote = (int)fwrite(&value, sizeof(value), 1, out);
    fclose(out);

    FILE *in = fopen("egtry_c17_binary_write.bin", "rb");
    int read = (int)fread(&readBack, sizeof(readBack), 1, in);
    fclose(in);
    remove("egtry_c17_binary_write.bin");

    printf("wrote=%d read=%d value=%d\n", wrote, read, readBack);
    return 0;
}
#include <stdio.h>

int main(void) {
    int value = 9;
    int readBack = 0;
    FILE *out = fopen("egtry_c17_binary_write.bin", "wb");
    int wrote = (int)fwrite(&value, sizeof(value), 1, out);
    fclose(out);

    FILE *in = fopen("egtry_c17_binary_write.bin", "rb");
    int read = (int)fread(&readBack, sizeof(readBack), 1, in);
    fclose(in);
    remove("egtry_c17_binary_write.bin");

    printf("wrote=%d read=%d value=%d\n", wrote, read, readBack);
    return 0;
}
#include <stdio.h>

int main(void) {
    int value = 12;
    int readBack = 0;
    FILE *out = fopen("egtry_c17_binary_write.bin", "wb");
    int wrote = (int)fwrite(&value, sizeof(value), 1, out);
    fclose(out);

    FILE *in = fopen("egtry_c17_binary_write.bin", "rb");
    int read = (int)fread(&readBack, sizeof(readBack), 1, in);
    fclose(in);
    remove("egtry_c17_binary_write.bin");

    printf("wrote=%d read=%d value=%d\n", wrote, read, readBack);
    return 0;
}
  1. value ← 7, readBack ← 0, out ← ⟨addr A⟩, wrote ← 1, in ← ⟨addr A⟩

    3int main(void) {4    int value→ 7 = 7; //@value=9, 125    int readBack→ 0 = 0;6    FILE *out→ ⟨addr A⟩ = fopen("egtry_c17_binary_write.bin", "wb");7    int wrote→ 1 = (int)fwrite(&value7, sizeof(value), 1, out⟨addr A⟩);8    fclose(out⟨addr A⟩);910    FILE *in→ ⟨addr A⟩ = fopen("egtry_c17_binary_write.bin", "rb");11    int read→ 1 = (int)fread(&readBack→ 7, sizeof(readBack), 1, in⟨addr A⟩);12    fclose(in⟨addr A⟩);13    remove("egtry_c17_binary_write.bin");1415    printf("wrote=%d read=%d value=%d\n", wrote1, read1, readBack7);16    return 0;17}
    outputwrote=1 read=1 value=7
  1. value ← 9, readBack ← 0, out ← ⟨addr A⟩, wrote ← 1, in ← ⟨addr A⟩

    3int main(void) {4    int value→ 9 = 9;5    int readBack→ 0 = 0;6    FILE *out→ ⟨addr A⟩ = fopen("egtry_c17_binary_write.bin", "wb");7    int wrote→ 1 = (int)fwrite(&value9, sizeof(value), 1, out⟨addr A⟩);8    fclose(out⟨addr A⟩);910    FILE *in→ ⟨addr A⟩ = fopen("egtry_c17_binary_write.bin", "rb");11    int read→ 1 = (int)fread(&readBack→ 9, sizeof(readBack), 1, in⟨addr A⟩);12    fclose(in⟨addr A⟩);13    remove("egtry_c17_binary_write.bin");1415    printf("wrote=%d read=%d value=%d\n", wrote1, read1, readBack9);16    return 0;17}
    outputwrote=1 read=1 value=9
  1. value ← 12, readBack ← 0, out ← ⟨addr A⟩, wrote ← 1, in ← ⟨addr A⟩

    3int main(void) {4    int value→ 12 = 12;5    int readBack→ 0 = 0;6    FILE *out→ ⟨addr A⟩ = fopen("egtry_c17_binary_write.bin", "wb");7    int wrote→ 1 = (int)fwrite(&value12, sizeof(value), 1, out⟨addr A⟩);8    fclose(out⟨addr A⟩);910    FILE *in→ ⟨addr A⟩ = fopen("egtry_c17_binary_write.bin", "rb");11    int read→ 1 = (int)fread(&readBack→ 12, sizeof(readBack), 1, in⟨addr A⟩);12    fclose(in⟨addr A⟩);13    remove("egtry_c17_binary_write.bin");1415    printf("wrote=%d read=%d value=%d\n", wrote1, read1, readBack12);16    return 0;17}
    outputwrote=1 read=1 value=12