内存映射文件问题
Memory mapped file problems
在我的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 %sn", ex.what());
fflush(stderr);
system("PAUSE");
return 0;
}
system("PAUSE");
return 0;
}
但是我得到了异常
异常:文件的卷被外部修改,打开的文件不再有效。
创建区域
时"mapped_region region(m_file, read_write)"
任何帮助都是非常感谢的。
谢谢
异常文件的卷被外部修改,打开的文件不再有效。
强烈提示文件在被映射时被另一个程序更改。并且错误信息表明发生的更改以不允许的方式影响大小。
避免其他程序写入文件,或有适当的同步和共享预防措施(如,不要改变大小,或只增长等)
您添加的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 %sn", ex.what());
fflush(stderr);
}
}
相关文章:
- 使用mongocxx驱动程序时包含头文件问题
- 在WSL:configure_file上对config_file的每次调用都失败:配置文件时出现问题
- 编译要在英特尔Hyperscan中使用的.cc文件时出现问题
- 使用 seekg() 读取C++中的文件时出现问题
- sqlite3 和生成文件中的链接出现问题
- C++头文件和类实现出现问题
- 用户输入字符串的文件附加问题..C++
- node-gyp 的先有鸡还是先有蛋的问题:指向依赖项中的头文件
- 读取文件并将其存储在unordered_map中时出现问题
- 编译 llvm 插件时出现问题:llvm/Config/llvm-config.h:没有这样的文件或目录
- 我在使用C++文件时遇到了一些问题
- 在Linux上使用Clang / OLLVM交叉编译helloworld Windows可执行文件时的问题
- 将文件复制到自定义位置,存在字符串转换问题
- DirectX12 的问题:"d3dApp.h":没有这样的文件或目录
- (ODR 使用问题)在不同文件中priority_queue名称相同的结构
- C++模板编程设计问题 - 根据输入文件返回不同的类型
- 读取制表符分隔的文件时出现问题 C++
- 当我在 CLion 中读取数组中的 txt 文件时C++编码问题
- 使用 RapidXML 解析大型 XML(大小大于 65 KB)文件时出现问题
- C++:读取.BMP文件时出现问题;文件结束时间早于预期