简体   繁体   中英

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:

//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. 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 !

I inspected boost/interprocess/details/os_file_functions.hpp but I didn't find a reason for that behavior.

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.

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) .

I didn't find that trick in all the page I read !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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