Boost iostream:如何将ifstream转换为内存映射文件?

发布于 2024-11-30 20:39:17 字数 943 浏览 0 评论 0原文

我想要的是简单地打开文件以作为内存映射文件进行读取 - 以便将来以更快的速度访问它(例如:我们打开文件读取它以结束,等待并一次又一次地读取它)同时我想要该文件可以被其他程序修改,当它们修改它时,我希望我的 ifstream 也能修改。如何使用 boost iostreams (或 boost interprocess )来做这样的事情?我们可以只是高操作系统 - 嘿这个文件应该为所有应用程序进行内存映射吗?

所以我尝试这样的代码:

#include <iostream> 
#include <boost/iostreams/device/mapped_file.hpp> 
#include <boost/iostreams/stream.hpp> 

using namespace boost::iostreams; 

int main(int argc, char **argv) 
{
    stream <mapped_file_sink> out; 
    try 
    { 

        mapped_file_params p("one.txt"); 
        p.new_file_size = 1024 * sizeof (char); 
        out.open(mapped_file_sink(p),  std::ios_base::out | std::ios_base::binary); 
    } 
    catch (const std::exception &e) 
    { 
        std::cout << e.what() << std::endl; 
        return 2; 
    } 
    std::cin.get();
    return 0; 
} 

它打开或创建文件,将其放入内存中。但我无法从任何其他程序访问它(我无法编辑和保存,但可以打开)=( 如何使文件可从其他程序编辑?

What I want is simple to open file for reading as memory mapped file - in order to access it with much more speed in future (example: we open file read it to end, wait and read it again and again) Meanwhile I want that file to be modifiable by other programms and when thay modify it I want my ifstream to modify too. How to do such thing with boost iostreams (or boost interprocess)? Can we just tall os - hey this file shall be memory mapped for all apps?

So I try such code:

#include <iostream> 
#include <boost/iostreams/device/mapped_file.hpp> 
#include <boost/iostreams/stream.hpp> 

using namespace boost::iostreams; 

int main(int argc, char **argv) 
{
    stream <mapped_file_sink> out; 
    try 
    { 

        mapped_file_params p("one.txt"); 
        p.new_file_size = 1024 * sizeof (char); 
        out.open(mapped_file_sink(p),  std::ios_base::out | std::ios_base::binary); 
    } 
    catch (const std::exception &e) 
    { 
        std::cout << e.what() << std::endl; 
        return 2; 
    } 
    std::cin.get();
    return 0; 
} 

so it opens or creates file, puts it into ram. But I can not access it (I cant edit and save but I can open) from any other programm=( How to make file editable from other programms?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我是男神闪亮亮 2024-12-07 20:39:17

我猜您正在寻找文件访问速度,但为什么要重新发明轮子呢?使用内存映射分区并在其中创建文件。然后您只需不时将它们同步到磁盘分区中,这样在断电的情况下就不会丢失信息...您始终可以投资 UPS...;-)

I guess that you're looking for file access speed but why reinventing the wheel? Use a memory mapped partition and create your files inside it. Then you just need to synch them into a disk partition from time to time so you don't lose info in case of a power failure... you can always invest on a UPS... ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文