簡體   English   中英

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

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

我正在移植源代碼來打開/讀取/寫入多個進程之間共享的文件。 它在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將作為文件而textBuffer二進制數據!

我檢查了boost/interprocess/details/os_file_functions.hpp但我沒有找到這種行為的原因。

你有個主意嗎?

最后,我找到了一個解決方案......如果你在文件的末尾( ::read之后的文件指針位置),那么在執行`boost :: interprocess ::時使用的::ftruncate函數detail :: truncate_file'導致錯誤的行為。

為了在Linux和Windows下保持相同的行為(保持我的文件類型為ASCII文本),我使用了一個簡單的::seek(id,0,SEEK_SET)

我沒有在我讀過的所有頁面中找到這個技巧!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM