gpt4 book ai didi

c - fmemopen 和 open_memstream 有什么区别?

转载 作者:IT王子 更新时间:2023-10-28 23:58:41 29 4
gpt4 key购买 nike

在阅读 string streams 上的 GNU 文档时我发现了两个相似的函数,它们做的事情非常相似:

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

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

Linux manpage explains :

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 有什么意义呢?

最佳答案

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

关于c - fmemopen 和 open_memstream 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29849749/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com