标准库标头 <fstream>

来自cppreference.com
< cpp‎ | header
 
 
标准库标头
注:修订记号中的反斜杠 '/' 意味着此标头被弃用和/或被移除。
语言支持
概念
<concepts> (C++20)
诊断
<system_error> (C++11)
内存管理
<memory_resource> (C++17)  
元编程
<type_traits> (C++11)
<ratio> (C++11)
通用工具
<utility>
<tuple> (C++11)
<optional> (C++17)
<variant> (C++17)
<any> (C++17)
<expected> (C++23)
<bitset>

<charconv> (C++17)
<format> (C++20)
<bit> (C++20)

字符串
<cuchar> (C++11)

容器
<flat_set> (C++23)
<span> (C++20)
<mdspan> (C++23)

迭代器
<iterator>
范围
<ranges> (C++20)
<generator> (C++23)
算法
数值
<cfenv> (C++11)
<complex>
<numbers> (C++20)

日期时间
<chrono> (C++11)
本地化
<codecvt> (C++11/17)
输入/输出
<sstream>
<spanstream> (C++23)
<fstream>
<syncstream> (C++20)
<filesystem> (C++17)
<cstdio>
<cinttypes> (C++11)
<strstream> (C++98/)
正则表达式
<regex>
并发支持
<stop_token> (C++20)
<thread> (C++11)
<atomic> (C++11)
<stdatomic.h> (C++23)
<mutex> (C++11)
<shared_mutex> (C++14)
<condition_variable> (C++11)  
<semaphore> (C++20)
<latch> (C++20)
<barrier> (C++20)
<future> (C++11)

C 兼容
<cstdbool> (C++11/17/20)  
<ccomplex> (C++11/17/20)
<ctgmath> (C++11/17/20)

<cstdalign> (C++11/17/20)

<ciso646> (C++20 前)

 

此头文件是输入/输出库的一部分。

抽象原生文件设备
(类模板)
实现高层文件流输入操作
(类模板)
实现高层文件流输出操作
(类模板)
实现高层文件流输入/输出操作
(类模板)
filebuf std::basic_filebuf<char>
(typedef)
wfilebuf std::basic_filebuf<wchar_t>
(typedef)
ifstream std::basic_ifstream<char>
(typedef)
wifstream std::basic_ifstream<wchar_t>
(typedef)
ofstream std::basic_ofstream<char>
(typedef)
wofstream std::basic_ofstream<wchar_t>
(typedef)
fstream std::basic_fstream<char>
(typedef)
wfstream std::basic_fstream<wchar_t>
(typedef)

函数

特化 std::swap 算法
(函数模板)
特化 std::swap 算法
(函数模板)
特化 std::swap 算法
(函数模板)
特化 std::swap 算法
(函数模板)

概要

namespace std {
  template<class charT, class traits = char_traits<charT>>
    class basic_filebuf;
  using filebuf  = basic_filebuf<char>;
  using wfilebuf = basic_filebuf<wchar_t>;
 
  template<class charT, class traits = char_traits<charT>>
    class basic_ifstream;
  using ifstream  = basic_ifstream<char>;
  using wifstream = basic_ifstream<wchar_t>;
 
  template<class charT, class traits = char_traits<charT>>
    class basic_ofstream;
  using ofstream  = basic_ofstream<char>;
  using wofstream = basic_ofstream<wchar_t>;
 
  template<class charT, class traits = char_traits<charT>>
    class basic_fstream;
  using fstream  = basic_fstream<char>;
  using wfstream = basic_fstream<wchar_t>;
}

类模板 std::basic_filebuf

namespace std {
  template<class CharT, class Traits = char_traits<CharT>>
  class basic_filebuf : public basic_streambuf<CharT, Traits> {
  public:
    using char_type   = CharT;
    using int_type    = typename Traits::int_type;
    using pos_type    = typename Traits::pos_type;
    using off_type    = typename Traits::off_type;
    using traits_type = Traits;
 
    // 构造函数/析构函数
    basic_filebuf();
    basic_filebuf(const basic_filebuf&) = delete;
    basic_filebuf(basic_filebuf&& rhs);
    virtual ~basic_filebuf();
 
    // 赋值与交换
    basic_filebuf& operator=(const basic_filebuf&) = delete;
    basic_filebuf& operator=(basic_filebuf&& rhs);
    void swap(basic_filebuf& rhs);
 
    // 成员
    bool is_open() const;
    basic_filebuf* open(const char* s, ios_base::openmode mode);
    basic_filebuf* open(const filesystem::path::value_type* s,
                        ios_base::openmode mode);   // 仅宽系统
    basic_filebuf* open(const string& s,
                        ios_base::openmode mode);
    basic_filebuf* open(const filesystem::path& s,
                        ios_base::openmode mode);
    basic_filebuf* close();
 
  protected:
    // 覆盖的虚函数
    streamsize showmanyc() override;
    int_type underflow() override;
    int_type uflow() override;
    int_type pbackfail(int_type c = Traits::eof()) override;
    int_type overflow (int_type c = Traits::eof()) override;
 
    basic_streambuf<CharT, Traits>* setbuf(char_type* s,
                                           streamsize n) override;
    pos_type seekoff(off_type off, ios_base::seekdir way,
                     ios_base::openmode which
                      = ios_base::in | ios_base::out) override;
    pos_type seekpos(pos_type sp,
                     ios_base::openmode which
                      = ios_base::in | ios_base::out) override;
    int      sync() override;
    void     imbue(const locale& loc) override;
  };
 
  template<class CharT, class Traits>
    void swap(basic_filebuf<CharT, Traits>& x,
              basic_filebuf<CharT, Traits>& y);
}

类模板 std::basic_ifstream

namespace std {
  template<class CharT, class Traits = char_traits<CharT>>
  class basic_ifstream : public basic_istream<CharT, Traits> {
  public:
    using char_type   = CharT;
    using int_type    = typename Traits::int_type;
    using pos_type    = typename Traits::pos_type;
    using off_type    = typename Traits::off_type;
    using traits_type = Traits;
 
    // 构造函数
    basic_ifstream();
    explicit basic_ifstream(const char* s,
                            ios_base::openmode mode = ios_base::in);
    explicit basic_ifstream(const filesystem::path::value_type* s,
                            ios_base::openmode mode = ios_base::in);// 仅宽系统
    explicit basic_ifstream(const string& s,
                            ios_base::openmode mode = ios_base::in);
    explicit basic_ifstream(const filesystem::path& s,
                            ios_base::openmode mode = ios_base::in);
    basic_ifstream(const basic_ifstream&) = delete;
    basic_ifstream(basic_ifstream&& rhs);
 
    // 赋值与交换
    basic_ifstream& operator=(const basic_ifstream&) = delete;
    basic_ifstream& operator=(basic_ifstream&& rhs);
    void swap(basic_ifstream& rhs);
 
    // 成员
    basic_filebuf<CharT, Traits>* rdbuf() const;
 
    bool is_open() const;
    void open(const char* s, ios_base::openmode mode = ios_base::in);
    void open(const filesystem::path::value_type* s,
              ios_base::openmode mode = ios_base::in);  // 仅宽系统
    void open(const string& s, ios_base::openmode mode = ios_base::in);
    void open(const filesystem::path& s, ios_base::openmode mode = ios_base::in);
    void close();
  private:
    basic_filebuf<CharT, Traits> sb;    // 仅用于阐释
  };
 
  template<class CharT, class Traits>
    void swap(basic_ifstream<CharT, Traits>& x,
              basic_ifstream<CharT, Traits>& y);
}

类模板 std::basic_ofstream

namespace std {
  template<class CharT, class Traits = char_traits<CharT>>
  class basic_ofstream : public basic_ostream<CharT, Traits> {
  public:
    using char_type   = CharT;
    using int_type    = typename Traits::int_type;
    using pos_type    = typename Traits::pos_type;
    using off_type    = typename Traits::off_type;
    using traits_type = Traits;
 
    // 构造函数
    basic_ofstream();
    explicit basic_ofstream(const char* s,
                            ios_base::openmode mode = ios_base::out);
    explicit basic_ofstream(const filesystem::path::value_type* s,  // 仅宽系统
                            ios_base::openmode mode = ios_base::out);
    explicit basic_ofstream(const string& s,
                            ios_base::openmode mode = ios_base::out);
    explicit basic_ofstream(const filesystem::path& s,
                            ios_base::openmode mode = ios_base::out);
    basic_ofstream(const basic_ofstream&) = delete;
    basic_ofstream(basic_ofstream&& rhs);
 
    // 赋值与交换
    basic_ofstream& operator=(const basic_ofstream&) = delete;
    basic_ofstream& operator=(basic_ofstream&& rhs);
    void swap(basic_ofstream& rhs);
 
    // 成员
    basic_filebuf<CharT, Traits>* rdbuf() const;
 
    bool is_open() const;
    void open(const char* s, ios_base::openmode mode = ios_base::out);
    void open(const filesystem::path::value_type* s,
              ios_base::openmode mode = ios_base::out);     // 仅宽系统
    void open(const string& s, ios_base::openmode mode = ios_base::out);
    void open(const filesystem::path& s, ios_base::openmode mode = ios_base::out);
    void close();
  private:
    basic_filebuf<CharT, Traits> sb;    // 仅用于阐释
  };
 
  template<class CharT, class Traits>
    void swap(basic_ofstream<CharT, Traits>& x,
              basic_ofstream<CharT, Traits>& y);
}

类模板 std::basic_fstream

namespace std {
  template<class CharT, class Traits = char_traits<CharT>>
  class basic_fstream : public basic_iostream<CharT, Traits> {
  public:
    using char_type   = CharT;
    using int_type    = typename Traits::int_type;
    using pos_type    = typename Traits::pos_type;
    using off_type    = typename Traits::off_type;
    using traits_type = Traits;
 
    // 构造函数
    basic_fstream();
    explicit basic_fstream(
      const char* s,
      ios_base::openmode mode = ios_base::in | ios_base::out);
    explicit basic_fstream(
      const filesystem::path::value_type* s,
      ios_base::openmode mode = ios_base::in | ios_base::out);    // 仅宽系统
    explicit basic_fstream(
      const string& s,
      ios_base::openmode mode = ios_base::in | ios_base::out);
    explicit basic_fstream(
      const filesystem::path& s,
      ios_base::openmode mode = ios_base::in | ios_base::out);
    basic_fstream(const basic_fstream&) = delete;
    basic_fstream(basic_fstream&& rhs);
 
    // 赋值与交换
    basic_fstream& operator=(const basic_fstream&) = delete;
    basic_fstream& operator=(basic_fstream&& rhs);
    void swap(basic_fstream& rhs);
 
    // 成员
    basic_filebuf<CharT, Traits>* rdbuf() const;
    bool is_open() const;
    void open(
      const char* s,
      ios_base::openmode mode = ios_base::in | ios_base::out);
    void open(
      const filesystem::path::value_type* s,
      ios_base::openmode mode = ios_base::in | ios_base::out);    // 仅宽系统
    void open(
      const string& s,
      ios_base::openmode mode = ios_base::in | ios_base::out);
    void open(
      const filesystem::path& s,
      ios_base::openmode mode = ios_base::in | ios_base::out);
    void close();
 
  private:
    basic_filebuf<CharT, Traits> sb;    // 仅宽系统
  };
 
  template<class CharT, class Traits>
    void swap(basic_fstream<CharT, Traits>& x,
              basic_fstream<CharT, Traits>& y);
}