簡體   English   中英

內存映射文件問題

[英]Memory mapped file problems

在我的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區域(m_file,讀_寫)”

任何幫助表示贊賞。

謝謝

異常文件的卷已在外部更改,因此打開的文件不再有效。

強烈建議文件在映射時被另一個程序更改。 並且錯誤消息指示更改恰好以不允許的方式影響了大小。

避免其他程序寫入文件,或避免進行適當的同步和共享注意事項(例如,不要更改大小,或僅增大大小等)。

更新

添加的SSCCE確認您在映射時保持文件打開:

您需要在映射文件之前關閉fbuf 另外,您需要先刪除映射,然后才能刪除它。

工作樣本:

生活在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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM