std::basic_ifstream
std::basic_ifstream (파일 입력 스트림)
파일 기반 스트림에 대한 고수준 입력 연산을 구현하는 클래스 템플릿이에요. 파일 기반 스트림 버퍼(std::basic_filebuf)와 고수준 인터페이스(std::basic_istream)를 연결해요.
출처: cppreference
본문
<fstream> 헤더에 정의돼 있어요.
template<
class CharT,
class Traits = std::char_traits<CharT>
> class basic_ifstream : public std::basic_istream<CharT, Traits>;
클래스 템플릿 basic_ifstream은 파일 기반 스트림에 대한 고수준 입력 연산을 구현해요. 파일 기반 스트림 버퍼(std::basic_filebuf)와 std::basic_istream의 고수준 인터페이스를 연결해요. 전형적인 구현은 std::basic_filebuf<CharT, Traits> 인스턴스 하나만 상속되지 않은 데이터 멤버로 보관해요.
공통 문자 타입 typedef
std::ifstream—std::basic_ifstream<char>std::wifstream—std::basic_ifstream<wchar_t>
멤버 타입
char_type,traits_type,int_type,pos_type,off_type.
멤버 함수
- (constructor) — ifstream을 생성해요(선택적으로 파일을 열어요).
- open — 파일을 열어요.
- is_open — 파일이 열려 있는지 확인해요.
- close — 파일을 닫아요.
- rdbuf — 기반 filebuf에 접근해요.
- operator= — 이동 대입.
예제
#include <fstream>
#include <iostream>
#include <string>
int main()
{
std::ifstream ifs("test.txt");
std::string s;
while (ifs >> s)
std::cout << s << ' ';
}