当前位置: 首页 > 工具软件 > Open MCT > 使用案例 >

fmemopen, open_memstream, open_wmemstream

沈开畅
2023-12-01

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
 类似资料:

相关阅读

相关文章

相关问答