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 <sys/mman.h> #include <stdio.h> #include <stdint.h> #include <fcntl.h> #include <unistd.h> void main () { // Create/open a file containing 1000 unsigned integers // Initialized to all zeros. int len = 1000 * sizeof ( uint32_t ); int file = open ( "numbers.u32" , O_RDWR | O_CREAT , 0600 ); ftruncate ( file , len ); // Map it into memory. uint32_t * numbers = mmap ( NULL , len , PROT_READ | PROT_WRITE , MAP_SHARED , file , 0 ); // Do something: printf ( "%d
" , 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