【问题标题】:Memory mapped file problems内存映射文件问题
【发布时间】:2016-01-14 23:34:56
【问题描述】:

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

我在 Windows Server 2008 R2 和 boost 1.58 上使用 Visual Studio 2010。

我从未使用过映射文件,所以我尝试在 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)"

感谢您的帮助。

谢谢

【问题讨论】:

  • 我看到错误导致您的大写锁定键表现得好像它卡在“活动”状态?

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


【解决方案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);
    }
}

【讨论】:

  • 您好,没有其他进程在使用该文件。该文件由示例创建...
  • 你在你的VS项目中添加了那个文件吗?谁给出错误?它在屏幕上是什么样子的?
  • 哦,好吧...这样就解决了:S 请参阅更新的答案。请注意,您介绍了问题。样品没有。课程:始终包含 SSCCE
  • 解决了这个问题!谢谢!我必须记住括号的实用性:P
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-17
  • 1970-01-01
  • 1970-01-01
  • 2016-02-23
  • 1970-01-01
  • 2010-11-01
  • 2011-05-14
相关资源
最近更新 更多