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

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

问题描述

字符串流上阅读GNU文档时>我发现有两个功能相似的功能:

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天全站免登陆