std::basic_fstream

std::basic_fstream (파일 입출력 스트림)

파일 기반 스트림에 대한 고수준 입출력 연산을 구현하는 클래스 템플릿이에요. 파일 기반 스트림 버퍼(std::basic_filebuf)와 고수준 인터페이스(std::basic_iostream)를 연결해요.

출처: cppreference

본문

<fstream> 헤더에 정의돼 있어요.

template<
    class CharT,
    class Traits = std::char_traits<CharT>
> class basic_fstream : public std::basic_iostream<CharT, Traits>;

클래스 템플릿 basic_fstream은 파일 기반 스트림에 대한 고수준 입출력 연산을 구현해요. 파일 기반 스트림 버퍼(std::basic_filebuf)와 std::basic_iostream의 고수준 인터페이스를 연결해요. 전형적인 구현은 std::basic_filebuf<CharT, Traits> 인스턴스 하나만 상속되지 않은 데이터 멤버로 보관해요.

공통 문자 타입 typedef

  • std::fstreamstd::basic_fstream<char>
  • std::wfstreamstd::basic_fstream<wchar_t>

멤버 타입

  • char_type, traits_type, int_type, pos_type, off_type.

멤버 함수

  • (constructor) — fstream을 생성해요(선택적으로 파일을 열어요).
  • open — 파일을 열어요.
  • is_open — 파일이 열려 있는지 확인해요.
  • close — 파일을 닫아요.
  • rdbuf — 기반 filebuf에 접근해요.
  • operator= — 이동 대입.

예제

#include <fstream>
#include <iostream>
#include <string>

int main()
{
    std::fstream fs("test.txt", std::ios::out | std::ios::in | std::ios::trunc);
    fs << "Hello, fstream!\n";
    fs.seekg(0);
    std::string s;
    std::getline(fs, s);
    std::cout << s << '\n'; // "Hello, fstream!"
}

더 알아보기 (Learn more)

cppreference