Namespaces
Variants

std:: fgetc, std:: getc

From cppreference.net
< cpp ‎ | io ‎ | c
헤더 파일에 정의됨 <cstdio>
int fgetc ( std:: FILE * stream ) ;
int getc ( std:: FILE * stream ) ;

주어진 입력 스트림에서 다음 문자를 읽습니다.

목차

매개변수

stream - 문자를 읽어올 스트림

반환값

성공 시 획득한 문자 또는 EOF 실패 시 반환값.

실패가 파일 끝 조건에 의해 발생한 경우, 추가적으로 eof 지시자를 설정합니다( std::feof() 참조). stream 에 대해. 실패가 다른 오류에 의해 발생한 경우, error 지시자를 설정합니다( std::ferror() 참조). stream 에 대해.

예제

#include <cstdio>
#include <cstdlib>
int main()
{
    int is_ok = EXIT_FAILURE;
    FILE* fp = std::fopen("/tmp/test.txt", "w+");
    if (!fp)
    {
        std::perror("File opening failed");
        return is_ok;
    }
    int c; // Note: int, not char, required to handle EOF
    while ((c = std::fgetc(fp)) != EOF) // Standard C I/O file reading loop
        std::putchar(c);
    if (std::ferror(fp))
        std::puts("I/O error when reading");
    else if (std::feof(fp))
    {
        std::puts("End of file reached successfully");
        is_ok = EXIT_SUCCESS;
    }
    std::fclose(fp);
    return is_ok;
}

출력:

End of file reached successfully

참고 항목

(C++11에서 사용 중단됨) (C++14에서 제거됨)
stdin 에서 문자 문자열을 읽음
(함수)
파일 스트림에 문자를 씀
(함수)
파일 스트림에 문자를 다시 넣음
(함수)
C 문서 를 참조하십시오: fgetc , getc