简体   繁体   English

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

[英]Under Linux boost::interprocess::create_or_open_file change the file type

I'm porting a source code to open/read/write a file shared between several process. 我正在移植源代码来打开/读取/写入多个进程之间共享的文件。 It works well under windows and as it was mainly using boost::interprocess (1.44) I didn't expecting too many issue but I found something weird: 它在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);

When the code run the first time it create the file and write a text. 当代码第一次运行时,它会创建文件并写入文本。 The file mode is ASCII ( ASCII text, with very long lines ) and I can read the text. 文件模式是ASCII( ASCII text, with very long lines ),我可以读取文本。 But when the loop run for the second times, the file type change to data and the textBuffer is in the file but as binary data ! 但是当循环第二次运行时,文件类型将更改为datatextBuffer将作为文件而textBuffer二进制数据!

I inspected boost/interprocess/details/os_file_functions.hpp but I didn't find a reason for that behavior. 我检查了boost/interprocess/details/os_file_functions.hpp但我没有找到这种行为的原因。

Have you an idea ? 你有个主意吗?

Finally, I found a solution.... It ssems that if you are at the end of the file (file pointer position after the ::read ), the ::ftruncate function used in the implementation of `boost::interprocess::detail::truncate_file' lead to the incorrect behavior. 最后,我找到了一个解决方案......如果你在文件的末尾( ::read之后的文件指针位置),那么在执行`boost :: interprocess ::时使用的::ftruncate函数detail :: truncate_file'导致错误的行为。

to keep the same behavior under Linux and Windows (keep my file type as ASCII Text), I used a simple ::seek(id,0,SEEK_SET) . 为了在Linux和Windows下保持相同的行为(保持我的文件类型为ASCII文本),我使用了一个简单的::seek(id,0,SEEK_SET)

I didn't find that trick in all the page I read ! 我没有在我读过的所有页面中找到这个技巧!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM