fmemopen

Autres langues

Langue: ja

Version: 2005-12-08 (openSuse - 09/10/07)

Section: 3 (Bibliothèques de fonctions)

̾Á°

fmemopen, open_memstream - ¥á¥â¥ê¤ò¥¹¥È¥ê¡¼¥à¤È¤·¤Æ¥ª¡¼¥×¥ó¤¹¤ë

½ñ¼°

#define _GNU_SOURCE
#include <stdio.h>

FILE *fmemopen(void *buf, size_t size, const char *mode);

FILE *open_memstream(char **ptr, size_t *sizeloc)

ÀâÌÀ

fmemopen() ´Ø¿ô¤Ï¡¢¥¹¥È¥ê¡¼¥à¤ò¥ª¡¼¥×¥ó¤·¡¢¤½¤Î¥¹¥È¥ê¡¼¥à¤Ë mode ¤Ç»ØÄꤵ¤ì¤¿¥¢¥¯¥»¥¹µö²Ä¤òÀßÄꤹ¤ë¡£ ¤½¤Î¥¹¥È¥ê¡¼¥à¤Ï¡¢ buf ¤Ç»ØÄꤵ¤ì¤¿Ê¸»úÎ󡢤Ĥޤê¥á¥â¥ê¥Ð¥Ã¥Õ¥¡¤È¤·¤ÆÆɤ߽ñ¤­¤Ç¤­¤ë¡£ ¤³¤Î¥Ð¥Ã¥Õ¥¡¤Ï¾¯¤Ê¤¯¤È¤â size ¥Ð¥¤¥È¤ÎŤµ¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£

°ú¤­¿ô mode ¤Ï fopen(3) ¤Î¾ì¹ç¤ÈƱ¤¸¤Ç¤¢¤ë¡£ mode ¤ÇÄɵ­¥â¡¼¥É (append mode) ¤¬»ØÄꤵ¤ì¤¿¾ì¹ç¡¢¥Õ¥¡¥¤¥ë°ÌÃ֤νé´üÃÍ¤Ï ¥Ð¥Ã¥Õ¥¡Ãæ¤ÎºÇ½é¤Î NULL ¥Ð¥¤¥È ('\0') ¤Î°ÌÃÖ¤ËÀßÄꤵ¤ì¤ë¡£ ¤½¤ì°Ê³°¤Î¾ì¹ç¤Ï¡¢¥Õ¥¡¥¤¥ë°ÌÃ֤νé´üÃͤϥХåե¡¤ÎÀèƬ¤Ë¤Ê¤ë¡£

½ñ¤­¹þ¤ßÍѤ˥ª¡¼¥×¥ó¤µ¤ì¤¿¥¹¥È¥ê¡¼¥à¤ò¥Õ¥é¥Ã¥·¥å (fflush(3)) ¤ä¥¯¥í¡¼¥º (fclose(3)) ¤·¤¿»þ¤Ë¡¢ (¥Ð¥Ã¥Õ¥¡¤Ë¶õ¤­¤¬¤¢¤ì¤Ð) NULL ¥Ð¥¤¥È¤¬¥Ð¥Ã¥Õ¥¡¤ÎËöÈø¤Ë½ñ¤­¹þ¤Þ¤ì¤ë¡£ ¤³¤Î¤è¤¦¤Ë¤¹¤ë¤¿¤á¤Ë¤Ï¡¢¸Æ¤Ó½Ð¤·¸µ¤Ï ¥Ð¥Ã¥Õ¥¡¤Ë 1¥Ð¥¤¥È;͵¤òºî¤ë (size ¤Ë¤³¤Î 1¥Ð¥¤¥È¤ò´Þ¤á¤¿Ãͤò»ØÄꤹ¤ë) ɬÍפ¬¤¢¤ë¡£

¥Ð¥Ã¥Õ¥¡¤Ë size ¥Ð¥¤¥È¤è¤ê¤¿¤¯¤µ¤ó½ñ¤­¹þ¤â¤¦¤È¤·¤¿¾ì¹ç¤Ë¤Ï¡¢¥¨¥é¡¼¤È¤Ê¤ë¡£ (¥Ç¥Õ¥©¥ë¥È¤Ç¤Ï¡¢¤³¤Î¤è¤¦¤Ê¥¨¥é¡¼¤Ï stdio ¥Ð¥Ã¥Õ¥¡¤¬¥Õ¥é¥Ã¥·¥å¤µ¤ì¤¿»þ¤Ë¤·¤«¸«¤¨¤Ê¤¤¡£ setbuf(fp, NULL) ¤ò»È¤Ã¤Æ¥Ð¥Ã¥Õ¥¡¥ê¥ó¥°¤ò̵¸ú¤Ë¤¹¤ëÊýË¡¤Ï¡¢ ½ÐÎÏÁàºî¤ò¹Ô¤Ã¤¿¤È¤­¤Ë¥¨¥é¡¼¤ò¸¡½Ð¤¹¤ë¤Î¤ËÌòΩ¤Ä¡£ Ê̤ÎÊýË¡¤È¤·¤Æ¤Ï¡¢ setbuffer(fp, buf, size) ¤ò»È¤Ã¤Æ¡¢¸Æ¤Ó½Ð¤·Â¦¤¬ÌÀ¼¨Åª¤Ë stdio ¥¹¥È¥ê¡¼¥à¥Ð¥Ã¥Õ¥¡¤È¤·¤Æ buf ¤ò»ØÄꤷ¡¢¥Ð¥Ã¥Õ¥¡¤Î»ØÄê»þ¤Ë¥Ð¥Ã¥Õ¥¡¤Î¥µ¥¤¥º¤ò stdio ¤Ë¶µ¤¨¤ëÊýË¡¤¬¤¢¤ë¡£)

Æɤ߽Ф·ÍѤ˥ª¡¼¥×¥ó¤µ¤ì¤¿¥¹¥È¥ê¡¼¥à¤Ç¤Ï¡¢ ¥Ð¥Ã¥Õ¥¡Æâ¤Ë NULL ¥Ð¥¤¥È ('\0') ¤¬¤¢¤Ã¤Æ¤â Æɤ߽Ф·Áàºî¤¬¥Õ¥¡¥¤¥ëËöÈø (end-of-file) ¤òÊÖ¤¹¤³¤È¤Ï¤Ê¤¤¡£ ¥Ð¥Ã¥Õ¥¡¤«¤é¤ÎÆɤ߽Ф·¤Ç¥Õ¥¡¥¤¥ëËöÈø¤¬ÊÖ¤ë¤Î¤Ï¡¢ ¥Õ¥¡¥¤¥ë¥Ý¥¤¥ó¥¿¤¬¥Ð¥Ã¥Õ¥¡¤ÎÀèƬ¤«¤é size ¥Ð¥¤¥È¤ò±Û¤¨¤ÆÀè¤Ë¿Ê¤â¤¦¤È¤·¤¿¾ì¹ç¤À¤±¤Ç¤¢¤ë¡£

buf ¤Ë NULL ¤¬»ØÄꤵ¤ì¤¿¾ì¹ç¡¢ fmemopen() ¤ÏưŪ¤Ë size ¥Ð¥¤¥È¤ÎŤµ¤Î¥Ð¥Ã¥Õ¥¡¤ò³ÎÊݤ¹¤ë¡£ ¤³¤ÎÊýË¡¤Ï¡¢°ì»þ¥Ð¥Ã¥Õ¥¡¤Ë¥Ç¡¼¥¿¤Î½ñ¤­¹þ¤ß¤ò¹Ô¤Ã¤Æ¤«¤é¡¢ ¤½¤ÎÆâÍƤòºÆÅÙÆɤ߽Ф¹¤è¤¦¤Ê¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤ÇÍ­ÍѤǤ¢¤ë¡£ ¤³¤Î¥Ð¥Ã¥Õ¥¡¤Ï¥¹¥È¥ê¡¼¥à¤¬¥¯¥í¡¼¥º¤µ¤ì¤ë¤È¤­¤Ë¼«Æ°Åª¤Ë²òÊü¤µ¤ì¤ë¡£ ¸Æ¤Ó½Ð¤·¸µ¤«¤é¤Ï¤³¤Î´Ø¿ô¤¬³ä¤êÅö¤Æ¤¿°ì»þ¥Ð¥Ã¥Õ¥¡¤Ø¤Î¥Ý¥¤¥ó¥¿Ãͤò ÃΤëÊýË¡¤Ï¸ºß¤·¤Ê¤¤ÅÀ¤ËÃí°Õ (²¼µ­¤Î open_memstream() ¤â»²¾È)¡£

open_memstream() ¤Ï¡¢¥Ð¥Ã¥Õ¥¡¤Ø¤Î½ñ¤­¹þ¤ßÍѤ˥¹¥È¥ê¡¼¥à¤ò¥ª¡¼¥×¥ó¤¹¤ë¡£ ¥Ð¥Ã¥Õ¥¡¤Ï (malloc(3) ¤ò»È¤Ã¤Æ) ưŪ¤Ë³ä¤êÅö¤Æ¤é¤ì¡¢É¬Íפ˱þ¤¸¤Æ¼«Æ°Åª¤Ë¿­Ä¹¤¹¤ë¡£ ¥¹¥È¥ê¡¼¥à¤ò¥¯¥í¡¼¥º¤·¤¿¸å¤Ç¡¢Æɤ߽Ф·¸µ¤Ï¤³¤Î¥Ð¥Ã¥Õ¥¡¤ò free(3) ¤¹¤Ù¤­¤Ç¤¢¤ë¡£

¤³¤Î¥¹¥È¥ê¡¼¥à¤¬ ¥¯¥í¡¼¥º (fclose(3)) ¤µ¤ì¤¿¤ê¥Õ¥é¥Ã¥·¥å (fflush(3)) ¤µ¤ì¤¿»þ¤Ë¡¢ ptr ¤È sizeloc ¤ÎÃͤϤ½¤ì¤¾¤ì¥Ð¥Ã¥Õ¥¡¤Ø¤Î¥Ý¥¤¥ó¥¿¤È¤½¤Î¥µ¥¤¥º¤Ë¹¹¿·¤µ¤ì¤ë¡£ ¤³¤ì¤é¤ÎÃͤϡ¢¸Æ¤Ó½Ð¤·¸µ¤¬¤½¤Î¥¹¥È¥ê¡¼¥à¤Ë¿·¤¿¤Ê½ñ¤­¹þ¤ß¤ò ¹Ô¤ï¤Ê¤¤¾ì¹ç¤Ë¸Â¤êÍ­¸ú¤Ç¤¢¤ë¡£ ¥¹¥È¥ê¡¼¥à¤Ë½ñ¤­¹þ¤ß¤ò¹Ô¤Ã¤¿ºÝ¤Ë¤Ï¡¢¤³¤ì¤é¤ÎÊÑ¿ô¤ò»²¾È¤¹¤ëÁ°¤Ë ¥¹¥È¥ê¡¼¥à¤òºÆÅÙ¥Õ¥é¥Ã¥·¥å¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£

¥Ð¥Ã¥Õ¥¡ËöÈø¤Î NULL ¥Ð¥¤¥È¤ÏÊÝ»ý¤µ¤ì¤ë¡£ ¤³¤Î NULL ¥Ð¥¤¥È¤Ï sizeloc ¤Ë³ÊǼ¤µ¤ì¤ë¥µ¥¤¥º¤Ë¤Ï¡Ö´Þ¤Þ¤ì¤Ê¤¤¡×¡£

ÊÖ¤êÃÍ

À®¸ù¤·¤Æ½ªÎ»¤·¤¿¾ì¹ç¤Ë¤Ï¡¢ fmemopen() ¤È open_memstream() ¤Ï FILE ¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£ ¼ºÇÔ¤·¤¿¾ì¹ç¤Ï¡¢ NULL ¤òÊÖ¤·¡¢Âç°èÊÑ¿ô errno ¤Ë¥¨¥é¡¼¤ò¼¨¤¹Ãͤò¥»¥Ã¥È¤¹¤ë¡£

½àµò

¤³¤ì¤é¤Î´Ø¿ô¤Ï GNU ³ÈÄ¥¤Ç¤¢¤ë¡£

Îã

¤³¤Î¥×¥í¥°¥é¥à¤Ï fmemopen() ¤ò»È¤Ã¤Æ½ÐÎϥХåե¡¤ò¥ª¡¼¥×¥ó¤·¡¢ open_memstream() ¤ò»È¤Ã¤ÆưŪ¤Ë¥µ¥¤¥º¤¬ÊѲ½¤¹¤ë½ÐÎϥХåե¡¤ò¥ª¡¼¥×¥ó¤·¤Æ¤¤¤ë¡£ (¥×¥í¥°¥é¥à¤ÎÂè°ì¥³¥Þ¥ó¥É¥é¥¤¥ó°ú¤­¿ô¤«¤é¼è¤Ã¤¿) ÆþÎÏʸ»úÎó¤ò ¥¹¥­¥ã¥ó¤·¤ÆÀ°¿ô¤òÆɤ߹þ¤ß¡¢¤³¤ì¤é¤ÎÀ°¿ô¤ÎÆó¾è¤ò½ÐÎϥХåե¡¤Ë½ñ¤­½Ð¤¹¡£ ¤³¤Î¥×¥í¥°¥é¥à¤Î¼Â¹ÔÎã¤Ï°Ê²¼¤Î¤è¤¦¤Ë¤Ê¤ë¡£



$ ./a.out "1 23 43"

size=11; ptr=1 529 1849



#define _GNU_SOURCE

#include <assert.h>

#include <string.h>

#include <stdio.h>

#include <stdlib.h>



int

main(int argc, char *argv[])

{

    FILE *out, *in;

    int v, s;

    size_t size;

    char *ptr;



    assert(argc == 2);



    in = fmemopen(argv[1], strlen(argv[1]), "r");

    if (in == NULL) { perror("fmemopen"); exit(EXIT_FAILURE);}



    out = open_memstream(&ptr, &size);

    if (out == NULL) { perror("fmemopen"); exit(EXIT_FAILURE);}



    for (;;) {

        s = fscanf(in, "%d", &v);

        if (s <= 0)

            break;



        s = fprintf(out, "%d ", v * v);

        if (s == -1) { perror("fprintf"); exit(EXIT_FAILURE); }

    }

    fclose(in);

    fclose(out);

    printf("size=%ld; ptr=%s\n", (long) size, ptr);

    free(ptr);

    exit(EXIT_SUCCESS);

}

´ØÏ¢¹àÌÜ

open(3), feature_test_macros(7)