[英]C++ FILE without writing to disk
I'm using a library that has quite a few functions that write to a FILE
but none that seem to conveniently dump the same data to an object in memory.我正在使用一个库,它有很多写入FILE
的函数,但似乎没有一个函数可以方便地将相同的数据转储到 memory 中的 object 中。 Is there any way to create a FILE
object (or override it) that stores the data in memory instead of writing to disk -- I'd like to avoid the performance hit of opening/writing/reading from files over and over again.有没有办法创建一个FILE
object(或覆盖它),将数据存储在 memory 中,而不是写入磁盘——我想避免一遍又一遍地打开/写入/读取文件的性能损失。
UPDATE: per Rob's suggestion, trying stringstream:更新:根据 Rob 的建议,尝试 stringstream:
ss.put(c);
std::string myval = ss.str();
printf("Value: %s\n after writing: %i length %lu\n",myval.c_str(),c, myval.length());
But, now trying to get the data (binary) out of the stringstream has me stuck -- how do I grab the binary data I've been adding?但是,现在试图从字符串流中获取数据(二进制)让我卡住了——如何获取我一直在添加的二进制数据?
Beside the already mentioned GNU's fmemopen()
, which is known in POSIX as open_memstream
, similar solution can be obtained combining
除了已经提到的 GNU 的mmap()
(using MAP_ANONYMOUS) or any other OS-specific function that returns a file descriptor to a block of memory, and
fdopen()
.
fmemopen()
在 POSIX 中称为open_memstream )之外
,可以通过组合
mmap()
(使用 MAP_ANONYMOUS)或任何其他特定于操作系统的 function 获得类似的解决方案,它将文件描述符返回到
fdopen()
块,
fdopen()
。
EDIT: that was wrong, mmap doesn't create a file descriptor.编辑:那是错误的, mmap 不会创建文件描述符。
The GNU libc has, eg, fmemopen
which will give you a FILE *
that writes to memory. GNU libc 有,例如fmemopen
,它会给你一个FILE *
写入 memory。 Try man fmemopen
on your Linux system for details.在您的 Linux 系统上尝试man fmemopen
以了解详细信息。
I suspect (but do not know for sure) that fmemopen
is a wrapper that orchestrates the mmap
/ fdopen
approach mentioned by @Cubbi.我怀疑(但不确定) fmemopen
是一个包装器,用于编排fdopen
提到的mmap
/ fdopen 方法。
If you are on Mac OS X or iOS you don't have access to fmemopen.如果您使用的是 Mac OS X 或 iOS,则您无权访问 fmemopen。 I've open sourced a solution here:我在这里开源了一个解决方案:
http://jverkoey.github.com/fmemopen/ http://jverkoey.github.com/fmemopen/
If you have the option of modifying your library, you could use C++ streams instead of C FILE streams.如果您可以选择修改库,则可以使用 C++ 流而不是 C 文件流。
If your old library function looked like this:如果您的旧库 function 看起来像这样:
void SomeFun(int this, int that, FILE* logger) {
... other code ...
fprintf(logger, "%d, %d\n", this, that);
fputs("Warning Message!", logger);
char c = '\n';
fputc(c, logger);
}
you might replace that code with:您可以将该代码替换为:
void SomeFun(int this, int that, std::ostream& logger) {
... other code ...
logger << this << ", " << that << "\n";
// or: logger << boost::format("%d %d\n") %this %that;
logger << "Warning Message!";
char c = '\n';
logger.put(c);
// or: logger << c;
}
Then, in your non-library code, do something like:然后,在您的非库代码中,执行以下操作:
#include <sstream>
std::stringstream logStream;
SomeFun(42, 56, logStream);
DisplayCStringOnGui(logStream.str().c_str());
https://github.com/Snaipe/fmem appears to be a portable fmemopen
in C. https://github.com/Snaipe/fmem似乎是 C 中的便携式fmemopen
。 It gives you FILE you can write to and when you done you get a void*
that points to the memory where you data is.它为您提供了您可以写入的文件,完成后您会得到一个void*
,它指向您的数据所在的 memory。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.