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 !