是否有可能创建一个C FILE对象来读取/写入内存 [英] Is it possible to create a C FILE object to read/write in memory

查看:255
本文介绍了是否有可能创建一个C FILE对象来读取/写入内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个接受 FILE * 的API,并使用它在内存中创建一个数据缓冲区:

  STD :: shared_ptr的<文件> f(tmpfile(),fclose); 
write_to_file(f.get());
rewind(f.get());
auto data = make_file_buffer(f.get());
返回数据;

这可以工作,但比写入内存缓冲区要慢。



是否有可能写入内存文件并避免读/写磁盘(比如stdin / stdout / stderr读/写到控制台)?

注意:我使用的是Linux,因此可以访问Linux和POSIX API。 是可能的,请参阅 fmemopen


I am using an API that takes a FILE * and am using that to create a data buffer in memory:

std::shared_ptr<FILE> f(tmpfile(), fclose);
write_to_file(f.get());
rewind(f.get());
auto data = make_file_buffer(f.get());
return data;

This works, but is slower than writing to a memory buffer.

Is it possible to get this to write to a memory file and avoid reading/writing to disk (like stdin/stdout/stderr read/write to the console)?

NOTE: I am using Linux, so have access to Linux and POSIX APIs.

解决方案

Yes, this is possible, see fmemopen.

这篇关于是否有可能创建一个C FILE对象来读取/写入内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆