반응형

fgetc(3) / getc(3) / getchar(3)

#include <stdio.h>

int fgetc(FILE *stream);
int getc(FILE *stream);
int getchar(void);

stream으로부터 1바이트의 데이터를 읽습니다. return 값은 (unsigned char)를 type casting한 데이터를 return합니다.  즉, 정상적인 값은 0 ~ 255의 숫자가 return 합니다. EOF가 return되면 end of file이거나 오류가 발생한 경우입니다.

getc(3)는 fgetc(3)의 macro함수이며 똑같은 기능을 합니다. getchar(3)은 fgetc(stdin)과 같은 함수입니다.

 

 

파라미터

stream
    - fopen(3), fdopen(3) 등으로 return 받은 FILE *입니다.

 

RETURN

0 ~ 255
    - 정상적으로 1byte의 데이터를 읽었습니다.
     정상적인 데이터는 (unsigned char)로 type casting하여 return 됩니다.

EOF
    - End of file에 도달하였거나 오류가 발생하였습니다.
    오류 내용은 errno 전역변수에 설정됩니다.

 


활용 예제

 

Sample

#include <stdio.h>
#include <string.h>
#include <errno.h>

int main(int argc, char *argv[])
{
    FILE *fp;
    int  ch;

    if((fp = fopen("mydata.dat", "r")) == NULL) {
        fprintf(stderr, "file open error: %s\n", strerror(errno));
        return 1;
    }

    while((ch = fgetc(fp)) != EOF) {
        printf("%c", ch);
    }

    fclose(fp);

    return 0;
}

 


see also :

    File 속성 정보 및 파일 관리 Library

    Directory 정보 조회 및 관리 Library

    System Call File I/O Library

    Stream File I/O Library

 

 

반응형
블로그 이미지

자연&사람

행복한 개발자 programmer since 1995.

,