在 Linux boost::interprocess::create_or_open_file 下更改文件类型

Under Linux boost::interprocess::create_or_open_file change the file type

我正在将源代码移植到 open/read/write 多个进程之间共享的文件。它在 windows 下运行良好,因为它主要使用 boost::interprocess (1.44) 我没想到会有太多问题,但我发现了一些奇怪的东西:

//pseudo code
namespace bip = boost::interprocess;
namespace bipd = boost::interprocess::detail;


loop
    bip::file_handle_t pFile = bipd::create_or_open_file(filename, bip::read_write);
    bipd::acquire_file_lock(pFile);  
    // try to read bytes from pFile with 'read' 
    bipd::truncate_file(pFile, 0);
    bipd::write_file(pFile, (const void*)(textBuffer)), bufLen);

当代码 运行 第一次创建文件并写入文本时。文件模式是 ASCII (ASCII text, with very long lines),我可以阅读文本。 但是当第二次循环 运行 时,文件类型更改为 data 并且 textBuffer 在文件中但作为二进制数据 !

我检查了 boost/interprocess/details/os_file_functions.hpp 但我没有找到该行为的原因。

你有想法吗?

终于,我找到了解决办法.... 它表明,如果你在文件的末尾(::read 之后的文件指针位置),::ftruncate 函数用于实现`boost::interprocess::detail::truncate_file' 导致不正确的行为。

为了在 Linux 和 Windows 下保持相同的行为(保持我的文件类型为 ASCII 文本),我使用了一个简单的 ::seek(id,0,SEEK_SET).

我在阅读的所有页面中都没有发现这个技巧!