NAME
fmemopen, open_memstream, open_wmemstream - open memory as stream
SYNOPSIS
#include <stdio.h> FILE *fmemopen(void *buf, size_t size, const char *mode); FILE *open_memstream(char **ptr, size_t *sizeloc); #include <wchar.h> FILE *open_wmemstream(wchar_t **ptr, size_t *sizeloc); fmemopen(), open_memstream(), open_wmemstream(): Since glibc 2.10: _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L Before glibc 2.10: _GNU_SOURCE
RETURN VALUE
Upon successful completion fmemopen(), open_memstream() and open_wmemstream() return a FILE pointer. Otherwise, NULL is returned and errno is set to indicate the error.VERSIONS
fmemopen() and open_memstream() were already available in glibc 1.0.x. open_wmemstream() is available since glibc 2.4.
Program source
#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define handle_error(msg) /
do { perror(msg); exit(EXIT_FAILURE); } while (0)
int
main(int argc, char *argv[])
{
FILE *out, *in;
int v, s;
size_t size;
char *ptr;
if (argc != 2) {
fprintf(stderr, "Usage: %s <file>/n", argv[0]);
exit(EXIT_FAILURE);
}
in = fmemopen(argv[1], strlen(argv[1]), "r");
if (in == NULL)
handle_error("fmemopen");
out = open_memstream(&ptr, &size);
if (out == NULL)
handle_error("open_memstream");
for (;;) {
s = fscanf(in, "%d", &v);
if (s <= 0)
break;
s = fprintf(out, "%d ", v * v);
if (s == -1)
handle_error("fprintf");
}
fclose(in);
fclose(out);
printf("size=%ld; ptr=%s/n", (long) size, ptr);
free(ptr);
exit(EXIT_SUCCESS);
}
Ref: http://www.kernel.org/doc/man-pages/online/pages/man3/fmemopen.3.html
Home: http://www.kernel.org/doc/man-pages/index.html
内存流操作详解
本文介绍如何使用fmemopen(), open_memstream() 和 open_wmemstream() 函数将内存区域作为文件流来操作的方法。这些函数允许程序员以文件的形式读写内存缓冲区,适用于多种场景,如临时文件处理和内存中的数据交换。
2155

被折叠的 条评论
为什么被折叠?



