C++ – Open a file in memory space

Open a file in memory space… here is a solution to the problem.

Open a file in memory space

[linux 3.2] I was wondering if it was possible to find out the memory location of the file opened in the program.
Let’s say I have the following code in a.cpp compiled to a.out:

FILE *f = fopen("myfile", "r");

Will the content be mapped to the memory space of a.out? If so, how can I find it (page?) Range? )?

Thanks

Solution

The structure pointed to by FILE* contains some information about the state of the file. The content does not map to the program’s memory space.

If you want to map the file contents to memory space, you must use the mmap() function. This will give you a pointer to a memory area in the process logical memory space. This memory area holds the contents of the file.

Related Problems and Solutions