way to open and load a file in memory
Is there a way such that I can open and load a file directly in memory? I have lot of legacy code that opens a file 开发者_运维问答present on network, and then does seeks and reads from this file. I want to avoid the reads and seeks over the network. Hence, if I could load the file in memory, when I open it, I could have efficient seeks. Any ideas? I'm working wtih C on Linux.
As Mat said, have a look at the mmap function. (It's probably the easier way) http://linux.die.net/man/2/mmap
If you prefer malloc, this link should help you: http://www.anyexample.com/programming/c/how_to_load_file_into_memory_using_plain_ansi_c_language.xml
You could use the mmap
function, or plain open
and read
it all into a buffer you've allocated with malloc
.
But please do benchmark. You might get very little (or none at all) improvement from this "manual buffering". The OS already does caching for you.
精彩评论