内存映射文件问题

时间:2015-10-16 09:16:50

标签: c++ visual-studio-2010 boost memory-mapped-files

在我的c ++代码中,我需要将大量数据写入文件,我想使用 boost映射文件而不是使用普通文件。只有当我在内存中写完所有数据时,我才想将映射文件一次性转储到磁盘上。

我在Windows Server 2008 R2上使用Visual Studio 2010并提升1.58。

我从未使用过映射文件,因此我尝试在boost文档中编译示例

#include <iostream>
#include <fstream>
#include <boost/interprocess/file_mapping.hpp>
#include <boost/interprocess/mapped_region.hpp>


int main(int argc, char** argv) 
{
    using namespace boost::interprocess;

const char* fileName = "C:\\logAcq\\test.bin";
const std::size_t fileSize = 10000;

std::cout << "create file" << std::endl;

try
{
    file_mapping::remove(fileName);
    std::filebuf fbuf;
    fbuf.open(fileName, std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::binary);

    std::cout << "set size" << std::endl;
    fbuf.pubseekoff(fileSize-1, std::ios_base::beg);
    fbuf.sputc(0);

    std::cout << "remove on exit" << std::endl;
    struct file_remove
    {
        file_remove(const char* fileName)
            :fileName_(fileName) {}
        ~file_remove(){ file_mapping::remove(fileName_); }
        const char *fileName_;
    }remover(fileName);

    std::cout << "create file mapping" << std::endl;
    file_mapping m_file(fileName, read_write);

    std::cout << "map the whole file" << std::endl;
    mapped_region region(m_file, read_write);

    std::cout << "get the address" << std::endl;
    void* addr = region.get_address();
    std::size_t size = region.get_size();

    std::cout << "write all memory to 1" << std::endl;
    memset(addr, 1, size);
}
catch (interprocess_exception &ex) 
{
    fprintf(stderr, "Exception %s\n", ex.what());
    fflush(stderr);
    system("PAUSE");

    return 0;
}

system("PAUSE");

return 0;
}

但是我得到了例外

异常文件的卷已经外部更改,因此打开的文件不再有效。

当我创建区域

“mapped_region region(m_file,read_write)”

任何帮助都表示赞赏。

由于

1 个答案:

答案 0 :(得分:1)

  

异常文件的卷已经外部更改,因此打开的文件不再有效。

强烈建议在映射时由另一个程序更改该文件。并且错误消息表明发生的更改会以不允许的方式影响大小。

避免其他程序写入文件,或采取适当的同步和共享预防措施(例如,不要改变大小,或只增长等)。

<强>更新

您添加的SSCCE确认您在映射时保持文件处于打开状态:

在映射文件之前,您需要关闭fbuf。此外,您需要在允许删除映射之前删除映射。

工作样本:

<强> Live On Coliru

#include <iostream>
#include <fstream>
#include <boost/interprocess/file_mapping.hpp>
#include <boost/interprocess/mapped_region.hpp>

int main() {
    using namespace boost::interprocess;

    const char *fileName = "test.bin";
    const std::size_t fileSize = 10000;

    std::cout << "create file " << fileName << std::endl;

    try {
        file_mapping::remove(fileName);
        {
            std::filebuf fbuf;
            fbuf.open(fileName, std::ios_base::in | std::ios_base::out | std::ios_base::trunc | std::ios_base::binary);

            std::cout << "set size" << std::endl;
            fbuf.pubseekoff(fileSize - 1, std::ios_base::beg);
            fbuf.sputc(0);
        }

        std::cout << "remove on exit" << std::endl;
        struct file_remove {
            file_remove(const char *fileName) : fileName_(fileName) {}
            ~file_remove() { file_mapping::remove(fileName_); }
            const char *fileName_;
        } remover(fileName);

        {
            std::cout << "create file mapping" << std::endl;
            file_mapping m_file(fileName, read_write);

            std::cout << "map the whole file" << std::endl;
            mapped_region region(m_file, read_write);

            std::cout << "get the address" << std::endl;
            void *addr = region.get_address();
            std::size_t size = region.get_size();

            std::cout << "write all memory to 1" << std::endl;
            memset(addr, 1, size);
        }
    } catch (interprocess_exception &ex) {
        fprintf(stderr, "Exception %s\n", ex.what());
        fflush(stderr);
    }
}