fmemopen 和 open_memstream 有什么区别? [英] What is the difference between fmemopen and open_memstream?

查看:28
本文介绍了fmemopen 和 open_memstream 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读关于 字符串流 我发现了两个相似的函数,它们做的事情非常相似:

While reading through the GNU documentation on string streams I found two similar functions that do very similar things:

FILE * fmemopen (void *buf, size_t size, const char *opentype)
FILE * open_memstream (char **ptr, size_t *sizeloc)

从阅读文档看来,open_memstream 应该用于打开输出流,fmemopen 用于输入.吸引我的是您可以传递给 fmemopenopentype 参数.

From reading the documentation, it seems open_memstream should be used for opening an output stream and fmemopen for input. What catches me is the opentype argument that you can pass to fmemopen.

linux 手册页解释:

如果 buf 指定为 NULL,则 fmemopen() 动态分配一个缓冲区大小字节长.这对于需要的应用程序很有用将数据写入临时缓冲区,然后再次读回.这当流关闭时,缓冲区会自动释放.注意调用者无法获得指向临时缓冲区的指针由该调用分配(但请参阅下面的 open_memstream()).

If buf is specified as NULL, then fmemopen() dynamically allocates a buffer size bytes long. This is useful for an application that wants to write data to a temporary buffer and then read it back again. The buffer is automatically freed when the stream is closed. Note that the caller has no way to obtain a pointer to the temporary buffer allocated by this call (but see open_memstream() below).

如果 fmemopen 可以打开输入/输出流,那么使用 open_memstream 有什么意义?

So what would be the point of using open_memstream if fmemopen can handle opening an input/output stream?

推荐答案

使用 fmemopen,缓冲区在打开时或之前分配,之后不会更改大小.如果你打算写它,你必须在开始之前知道你的输出有多大.使用 open_memstream,缓冲区会随着您的写入而增长.

With fmemopen, the buffer is allocated at or before the open, and doesn't change size later. If you're going to write to it, you have to know how big your output will be before you start. With open_memstream the buffer grows as you write.

这篇关于fmemopen 和 open_memstream 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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