fmemopen, open_memstream, open_wmemstream

长平狐 发布于 2013/01/06 11:20
阅读 255
收藏 0

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

原文链接:http://blog.csdn.net/sunboy_2050/article/details/6121069
加载中
返回顶部
顶部