mmap - one-liner to read file to string
mmap stands for memory map. the declaration1 is given below:
#include <sys/mman.h> void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset); int munmap(void *addr, size_t length);
- My article is partly inspired by Jacob Sorber’s How to Map Files into Memory in C (mmap, memory mapped file io)
mmap vs. other ways
there are several ways to read files in C, none you could do without several line of code and iteration. the quickest approach would be
fgets() within a loop.
mmap offers a simple one-liner. recently I’ve been working with OpenCL, which is known for its unconventional practice to load kernel as a string and compile and run it during runtime.
reading file to string with mmap
we need to know the exact size of the file we are going to read. we can do that with
#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <sys/mman.h> #include <sys/stat.h> int k_fd = open("kernel.cl", O_RDONLY); struct stat s; fstat(k_fd, &s); printf("%d\n", s.st_size);
then we need to allocate memory and call
char *kernelSource = (char *)malloc(s.st_size * sizeof(char)); kernelSource = (char *)mmap(0, s.st_size, PROT_READ, MAP_PRIVATE, k_fd, 0);
mmap returns a void pointer, it’s necessary to cast it to a
It looks better and feels good (at least to me 😉) than other ways of reading files. sharing this short example with others who might be looking for it.