getline

Autres langues

Langue: ja

Version: 2006-05-17 (openSuse - 09/10/07)

Section: 3 (Bibliothèques de fonctions)

̾Á°

getline, getdelim - ¶èÀÚ¤êʸ»ú¤Þ¤Ç¤Îʸ»úÎóÆþÎϤòÆɤ߹þ¤à

½ñ¼°


#define _GNU_SOURCE

#include <stdio.h>



ssize_t getline(char **lineptr, size_t *n, FILE *stream);


ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
 

ÀâÌÀ

getline() ¤Ï stream ¤«¤é 1 ¹ÔÁ´¤Æ¤òÆɤ߹þ¤ß¡¢¥Æ¥­¥¹¥È¤¬´Þ¤Þ¤ì¤Æ¤¤¤ë¥Ð¥Ã¥Õ¥¡¤Î¥¢¥É¥ì¥¹¤ò *lineptr ¤Ë³ÊǼ¤¹¤ë¡£ ¥Ð¥Ã¥Õ¥¡¤Ï¥Ì¥ëʸ»ú (\0) ¤Ç½ªÃ¼¤µ¤ì¤ë¡£ ²þ¹Ôʸ»ú¤¬¸«¤Ä¤«¤Ã¤¿¾ì¹ç¤Ï¡¢²þ¹Ôʸ»ú¤â¥Ð¥Ã¥Õ¥¡¤Ë³ÊǼ¤µ¤ì¤ë¡£

*lineptr ¤¬ NULL ¤Î¾ì¹ç¡¢ getline() ¤Ï¹Ô¤ÎÆâÍƤò³ÊǼ¤¹¤ë¤¿¤á¤Î¥Ð¥Ã¥Õ¥¡¤ò³ÎÊݤ¹¤ë¡£ ¤³¤Î¥Ð¥Ã¥Õ¥¡¤Ï¥æ¡¼¥¶¡¼¤Î¥×¥í¥°¥é¥à¤Ç²òÊü¤¹¤Ù¤­¤Ç¤¢¤ë¡£ Ê̤ÎÊýË¡¤È¤·¤Æ¡¢ getline() ¤ò¸Æ¤Ó½Ð¤¹ºÝ¤Ë¡¢ *lineptr ¤Ë malloc(3) ¤Ç³ÎÊݤ·¤¿Â礭¤µ *n ¥Ð¥¤¥È¤Î¥Ð¥Ã¥Õ¥¡¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÆþ¤ì¤ÆÅϤ¹¤³¤È¤â¤Ç¤­¤ë¡£ Æɤ߹þ¤ó¤À¹Ô¤òÊÝ»ý¤¹¤ë¤Î¤Ë½½Ê¬¤Ê¥Ð¥Ã¥Õ¥¡¤¬¤Ê¤¤¾ì¹ç¡¢ getline() ¤Ï realloc(3) ¤ò»È¤Ã¤Æ¥Ð¥Ã¥Õ¥¡¤Î¥µ¥¤¥º¤òÊѹ¹¤·¡¢É¬Íפ˱þ¤¸¤Æ *lineptr ¤È *n ¤ò¹¹¿·¤¹¤ë¡£ ¤É¤Á¤é¤Î¾ì¹ç¤Ç¤â¡¢¸Æ¤Ó½Ð¤·¤ËÀ®¸ù¤·¤¿¤È¤­¤Ë¤Ï¡¢ *lineptr ¤È *n ¤ò¥Ð¥Ã¥Õ¥¡¤Î¥¢¥É¥ì¥¹¤È³ä¤êÅö¤Æ¤¿¥µ¥¤¥º¤òÈ¿±Ç¤·¤¿Ãͤ˹¹¿·¤¹¤ë¡£

getdelim() ¤Ï getline() ¤ÈƱ¤¸¤è¤¦¤ËÆ°ºî¤¹¤ë¤¬¡¢²þ¹Ôʸ»ú°Ê³°¤Î¶èÀÚ¤êʸ»ú¤ò°ú¤­¿ô delim ¤Ë»ØÄꤹ¤ë¤³¤È¤¬¤Ç¤­¤ë¡£ getline() ¤ÈƱÍͤˡ¢¥Õ¥¡¥¤¥ë½ªÃ¼¤Ë㤹¤ë¤Þ¤Ç¤ËÆþÎϹԤ˶èÀÚ¤êʸ»ú¤¬¸«ÉÕ¤«¤é¤Ê¤¤¾ì¹ç¤Ï¡¢ ¶èÀÚ¤êʸ»ú¤ò¥Ð¥Ã¥Õ¥¡¤ËÄɲ䷤ʤ¤¡£

ÊÖ¤êÃÍ

À®¸ù¤·¤¿¾ì¹ç¡¢ getline() ¤È getdelim() ¤ÏÆɤ߹þ¤ó¤Àʸ»ú¿ô¤òÊÖ¤¹¡£ ʸ»ú¿ô¤Ë¤Ï¶èÀÚ¤êʸ»ú¤ò´Þ¤à¤¬¡¢½ªÃ¼¤Ë»È¤¦ NULL ¥Ð¥¤¥È¤Ï´Þ¤Þ¤Ê¤¤¡£ ¤³¤ÎÃͤˤè¤Ã¤Æ¡¢Æɤ߹þ¤ó¤À¹Ô¤Ë´Þ¤Þ¤ì¤ë NULL ¥Ð¥¤¥È¤òÁàºî¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¡£

¤É¤Á¤é¤Î´Ø¿ô¤â¡¢¹Ô¤ÎÆɤ߹þ¤ß¤Ë¼ºÇÔ¤·¤¿¾ì¹ç¤Ë¤Ï -1 ¤òÊÖ¤¹ (¥Õ¥¡¥¤¥ë¤Î½ªÃ¼¤Ë㤷¤¿¾ì¹ç¤Ë¤â -1 ¤òÊÖ¤¹)¡£

¥¨¥é¡¼

EINVAL
¥Ñ¥é¥á¡¼¥¿¤¬ÉÔÀµ¤Ç¤¢¤ë (n ¤Þ¤¿¤Ï lineptr ¤¬ NULL ¤Ç¤¢¤ë¡£ ¤â¤·¤¯¤Ï stream ¤¬Í­¸ú¤Ç¤Ê¤¤)¡£

½àµò

getline() ¤È getdelim() ¤Ï¡¢¤È¤â¤Ë GNU ¤Ë¤è¤ë³ÈÄ¥¤Ç¤¢¤ë¡£ ¤³¤ì¤é¤Î´Ø¿ô¤Ï libc 4.6.27 °Ê¹ß¤Ç»ÈÍѲÄǽ¤Ç¤¢¤ë¡£

Îã


#define _GNU_SOURCE

#include <stdio.h>

#include <stdlib.h>



int

main(void)

{

    FILE * fp;

    char * line = NULL;

    size_t len = 0;

    ssize_t read;

    fp = fopen("/etc/motd", "r");

    if (fp == NULL)

        exit(EXIT_FAILURE);

    while ((read = getline(&line, &len, fp)) != -1) {

        printf("Retrieved line of length %zu :\n", read);

        printf("%s", line);

    }

    if (line)

        free(line);

    return EXIT_SUCCESS;

}

´ØÏ¢¹àÌÜ

read(2), fgets(3), fopen(3), fread(3), gets(3), scanf(3), feature_test_macros(7)