开发者

Boost iostream: how to turn ifstream into memory mapped file?

开发者 https://www.devze.com 2023-03-30 05:24 出处:网络
What I want is simple to open file for reading as memory mapped file - in order to access it with much more speed in future (example: we open file read it to end, wait and read it again and again) Mea

What I want is simple to open file for reading as memory mapped file - in order to access it with much more speed in future (example: we open file read it to end, wait and read it again and again) Meanwhile I want that file to be modifiable by other programms and when thay modify it I want my ifstream to modify too. How to do such thing with boost iostreams (or boost interprocess)? Can we just tall os - hey this file shall be memory mapped for all apps?

So I try such code:

#include <iostream> 
#include <boost/iostreams/device/mapped_file.hpp> 
#include <boost/iostreams/stream.hpp> 

using namespace boost::iostreams; 

int main(int开发者_运维技巧 argc, char **argv) 
{
    stream <mapped_file_sink> out; 
    try 
    { 

        mapped_file_params p("one.txt"); 
        p.new_file_size = 1024 * sizeof (char); 
        out.open(mapped_file_sink(p),  std::ios_base::out | std::ios_base::binary); 
    } 
    catch (const std::exception &e) 
    { 
        std::cout << e.what() << std::endl; 
        return 2; 
    } 
    std::cin.get();
    return 0; 
} 

so it opens or creates file, puts it into ram. But I can not access it (I cant edit and save but I can open) from any other programm=( How to make file editable from other programms?


I guess that you're looking for file access speed but why reinventing the wheel? Use a memory mapped partition and create your files inside it. Then you just need to synch them into a disk partition from time to time so you don't lose info in case of a power failure... you can always invest on a UPS... ;-)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号