【问题标题】:Under Linux boost::interprocess::create_or_open_file change the file typeLinux下 boost::interprocess::create_or_open_file 改变文件类型
【发布时间】:2015-02-24 16:48:06
【问题描述】:

我正在移植源代码以打开/读取/写入在多个进程之间共享的文件。它在 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),我可以阅读文本。 但是当循环第二次运行时,文件类型更改为 datatextBuffer 在文件中,但作为二进制数据!

我检查了boost/interprocess/details/os_file_functions.hpp,但没有找到导致这种行为的原因。

你有什么想法吗?

【问题讨论】:

    标签: c++ linux file boost-interprocess


    【解决方案1】:

    最后,我找到了解决方案.... 它ssems,如果你在文件的末尾(::read 之后的文件指针位置),在 `boost::interprocess::detail::truncate_file' 的实现中使用的::ftruncate 函数会导致不正确的行为.

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

    我没有在我阅读的所有页面中找到那个技巧!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-19
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多