简体   繁体   中英

boost::iostreams::mapped_file file path issue

I ran into an issue using the mapped_file iostreams library of boost.

boost documentation : mapped_file

After reading through all the documentation and examples I could read, I still can't get the following piece of code to work . I assume that when you pass a new_file_size and a path to the mapped_file_params class , it will then open(and create) the file at the location desired when I call the constructor of mapped_file with the mapped_file_params object in parameter. But the only thing it does is to create a file with a weird name , like "%F8/134" in the project working directory, not where I told it to. Maybe I'm wrong and one should create file before specifying it to the mapped_file? After reading the doc, I was under the assumption that it created a new file when you fill in the new_file_size param. and specifies the filename.

Anyone experienced this before? Any advice on how to resolve this?

Here's the code:

boost::iostreams::mapped_file_params param(filename); // filename is a std::string with the correct path
param.flags = boost::iostreams::mapped_file::mapmode::readwrite;
param.new_file_size = sizeNeeded; // sizeNeeded is the size of the data
param.length = sizeNeeded;
boost::iostreams::mapped_file fileMap(param); // at this point the file is created
…

Thanks for your time,

Alexandre

First, I'm not sure how well mapmode::readwrite is supported and how well boost::iostream library is maintained. For my curiosity I tried your slightly "modified" code with boost 1.52 on Ubuntu 12.04 compiled with g++-4.7.2. I have not seen the issue with "strange name". The file was created with proper name but mf.open( param ) had not finished for raised exception on mmap in file: mapped_file.cpp

void* data = 
    ::BOOST_IOSTREAMS_FD_MMAP( 
        const_cast<char*>(p.hint), 
        size_,
        readonly ? PROT_READ : (PROT_READ | PROT_WRITE),
        priv ? MAP_PRIVATE : MAP_SHARED,
        handle_, 
        p.offset );
if (data == MAP_FAILED)
    cleanup_and_throw("failed mapping file");

If you can upgrade to more recent version I would do so. In addition you can build boost library in debug mode and trace the code to see what gets wrong. Here you can find some hints to build boost libraries in debug mode

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM