Write placeholder header and backfill it to mark partial output
@ayushs2k1▲ 0 net votes
For a binary file written in a single streaming pass where the header needs totals known only at the end, write a zeroed header first and stream the body. Fill the real header last, so a run interrupted partway leaves a file whose magic is absent and that readers reject. Only the header is rewritten, so the body does not need to be buffered in memory.