Why does C have the best file API?
2026-02-28
2026-03-01
Ok, the title is a bit tongue-in-cheek, but there's very little thought put into files in most languages. It always feels a bit out of place... except in C. In fact, what you get is usually a worse version of C.
In C, files can be accessed in the same way as memory:
#include
" , numbers [ 42 ]); numbers [ 42 ] = numbers [ 42 ] + 1 ; // Clean up munmap ( numbers , len ); close ( file ); }
Memory mapping isn't the same as loading a file into memory: It still works if the file doesn't fit in RAM. Data is loaded as needed, so it won't take all day to open a terabyte file.
It works with all datatypes and is automatically cached. This cache is cleared if the system needs memory for something else.
mmap() is actually a OS feature, so many other languages have it. However, it's almost always limited to byte arrays: You have to grab a chunk of data, parse, process and finally serialize it before writing back to the disk. It's nicer then manually calling read() and write(), but not by much.
... continue reading