std:: getchar
| I/O manipulators | ||||
| Print functions (C++23) | ||||
| C-style I/O | ||||
| Buffers | ||||
|
(C++23)
|
||||
|
(
C++98/26*
)
|
||||
|
(C++20)
|
||||
| Streams | ||||
| Abstractions | ||||
| File I/O | ||||
| String I/O | ||||
| Array I/O | ||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
(C++23)
|
||||
|
(
C++98/26*
)
|
||||
|
(
C++98/26*
)
|
||||
|
(
C++98/26*
)
|
||||
| Synchronized Output | ||||
|
(C++20)
|
||||
| Types | ||||
| Error category interface | ||||
|
(C++11)
|
||||
|
(C++11)
|
| Types and objects | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
헤더 파일에 정의됨
<cstdio>
|
||
|
int
getchar
(
)
;
|
||
stdin 에서 다음 문자를 읽어옵니다.
std:: getc ( stdin ) 와 동등합니다.
목차 |
매개변수
(없음)
반환값
성공 시 획득한 문자 또는 EOF 실패 시 반환됩니다.
실패가 파일 끝 조건에 의해 발생한 경우, 추가적으로 eof 표시자를 설정합니다( std::feof() 참조). 실패가 다른 오류에 의해 발생한 경우, error 표시자를 설정합니다( std::ferror() 참조).
예제
std::getchar
오류 검사와 함께. ESC 문자를 입력하여 프로그램 종료.
#include <cctype> #include <cstdio> #include <cstdlib> #include <iomanip> #include <iostream> int main() { for (int ch; (ch = std::getchar()) != EOF ;) // stdin에서 "abc" 읽기/출력 { if (std::isprint(ch)) std::cout << static_cast<char>(ch) << '\n'; if (ch == 27) // ASCII에서 'ESC' (이스케이프) return EXIT_SUCCESS; } // EOF 도달 원인 테스트 if (std::feof(stdin)) // 실패가 파일 끝 조건으로 인한 경우 std::cout << "파일 끝에 도달함\n"; else if (std::ferror(stdin)) // 실패가 다른 오류로 인한 경우 { std::perror("getchar()"); std::cerr << "getchar()가 파일 " << std::quoted(__FILE__) << "의 라인 # " << __LINE__ - 14 << "에서 실패함\n"; std::exit(EXIT_FAILURE); } return EXIT_SUCCESS; }
가능한 출력:
abc a b c ^[
참고 항목
|
파일 스트림에서 문자를 가져옴
(함수) |
|
|
C 문서
for
getchar
|
|