Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/header/fstream"

From cppreference.com
< cpp‎ | header
m (link to zh)
m (~{{c/core}})
 
(7 intermediate revisions by 5 users not shown)
Line 4: Line 4:
 
{{dsc begin}}
 
{{dsc begin}}
 
{{dsc h1|Classes}}
 
{{dsc h1|Classes}}
{{dsc tclass | cpp/io/basic_filebuf | implements raw file device}}
+
{{dsc inc|cpp/io/dsc basic_filebuf}}
{{dsc tclass | cpp/io/basic_ifstream | implements high-level file stream input operations}}
+
{{dsc inc|cpp/io/dsc basic_ifstream}}
{{dsc tclass | cpp/io/basic_ofstream | implements high-level file stream output operations}}
+
{{dsc inc|cpp/io/dsc basic_ofstream}}
{{dsc tclass | cpp/io/basic_fstream | implements high-level file stream input/output}}
+
{{dsc inc|cpp/io/dsc basic_fstream}}
{{dsc h1|Typedefs}}
+
{{dsc|{{tt|filebuf}}|{{c/core|std::basic_filebuf<char>}}<br>{{mark typedef}}}}
{{dsc | {{tt|filebuf}} | {{c|basic_filebuf<char>}}}}
+
{{dsc|{{tt|wfilebuf}}|{{c/core|std::basic_filebuf<wchar_t>}}<br>{{mark typedef}}}}
{{dsc | {{tt|wfilebuf}} | {{c|basic_filebuf<wchar_t>}}}}
+
{{dsc|{{tt|ifstream}}|{{c/core|std::basic_ifstream<char>}}<br>{{mark typedef}}}}
{{dsc | {{tt|ifstream}} | {{c|basic_ifstream<char>}}}}
+
{{dsc|{{tt|wifstream}}|{{c/core|std::basic_ifstream<wchar_t>}}<br>{{mark typedef}}}}
{{dsc | {{tt|wifstream}} | {{c|basic_ifstream<wchar_t>}}}}
+
{{dsc|{{tt|ofstream}}|{{c/core|std::basic_ofstream<char>}}<br>{{mark typedef}}}}
{{dsc | {{tt|ofstream}} | {{c|basic_ofstream<char>}}}}
+
{{dsc|{{tt|wofstream}}|{{c/core|std::basic_ofstream<wchar_t>}}<br>{{mark typedef}}}}
{{dsc | {{tt|wofstream}} | {{c|basic_ofstream<wchar_t>}}}}
+
{{dsc|{{tt|fstream}}|{{c/core|std::basic_fstream<char>}}<br>{{mark typedef}}}}
{{dsc | {{tt|fstream}} | {{c|basic_fstream<char>}}}}
+
{{dsc|{{tt|wfstream}}|{{c/core|std::basic_fstream<wchar_t>}}<br>{{mark typedef}}}}
{{dsc | {{tt|wfstream}} | {{c|basic_fstream<wchar_t>}}}}
+
 
{{dsc h1|Functions}}
 
{{dsc h1|Functions}}
{{dsc inc | cpp/io/basic_filebuf/dsc swap2}}
+
{{dsc inc|cpp/io/basic_filebuf/dsc swap2}}
{{dsc inc | cpp/io/basic_fstream/dsc swap2 | basic_ifstream}}
+
{{dsc inc|cpp/io/basic_fstream/dsc swap2|basic_ifstream}}
{{dsc inc | cpp/io/basic_fstream/dsc swap2 | basic_ofstream}}
+
{{dsc inc|cpp/io/basic_fstream/dsc swap2|basic_ofstream}}
{{dsc inc | cpp/io/basic_fstream/dsc swap2 | basic_fstream}}
+
{{dsc inc|cpp/io/basic_fstream/dsc swap2|basic_fstream}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===Synopsis===
 
===Synopsis===
 +
{{cpp/synopsis/fstream}}
  
{{source|1=
+
{{langlinks|es|ja|zh}}
namespace std {
+
 
+
template <class charT, class traits = char_traits<charT> >
+
    class basic_filebuf;
+
typedef basic_filebuf<char> filebuf;
+
typedef basic_filebuf<wchar_t> wfilebuf;
+
template <class charT, class traits>
+
    void swap(basic_filebuf<charT, traits>& x,
+
              basic_filebuf<charT, traits>& y);
+
 
+
template <class charT, class traits = char_traits<charT> >
+
    class basic_ifstream;
+
typedef basic_ifstream<char> ifstream;
+
typedef basic_ifstream<wchar_t> wifstream;
+
template <class charT, class traits>
+
    void swap(basic_ifstream<charT, traits>& x,
+
              basic_ifstream<charT, traits>& y);
+
 
+
template <class charT, class traits = char_traits<charT> >
+
    class basic_ofstream;
+
typedef basic_ofstream<char> ofstream;
+
typedef basic_ofstream<wchar_t> wofstream;
+
template <class charT, class traits>
+
    void swap(basic_ofstream<charT, traits>& x,
+
              basic_ofstream<charT, traits>& y);
+
 
+
template <class charT, class traits = char_traits<charT> >
+
    class basic_fstream;
+
typedef basic_fstream<char> fstream;
+
typedef basic_fstream<wchar_t> wfstream;
+
template <class charT, class traits>
+
    void swap(basic_fstream<charT, traits>& x,
+
              basic_fstream<charT, traits>& y);
+
}
+
}}
+
 
+
====Class {{lc|std::basic_filebuf}}====
+
{{source|1=
+
template <class charT, class traits = char_traits<charT> >
+
class basic_filebuf : public basic_streambuf<charT,traits> {
+
public:
+
    typedef charT char_type;
+
    typedef typename traits::int_type int_type;
+
    typedef typename traits::pos_type pos_type;
+
    typedef typename traits::off_type off_type;
+
    typedef traits traits_type;
+
 
+
    // Constructors/destructor:
+
    basic_filebuf();
+
    basic_filebuf(const basic_filebuf& rhs) = delete;
+
    basic_filebuf(basic_filebuf&& rhs);
+
    virtual ~basic_filebuf();
+
 
+
    // Assign/swap:
+
    basic_filebuf& operator=(const basic_filebuf& rhs) = delete;
+
    basic_filebuf& operator=(basic_filebuf&& rhs);
+
    void swap(basic_filebuf& rhs);
+
 
+
    // Members:
+
    bool is_open() const;
+
    basic_filebuf<charT,traits>* open(const char* s, ios_base::openmode mode);
+
    basic_filebuf<charT,traits>* open(const string& s, ios_base::openmode mode);
+
    basic_filebuf<charT,traits>* close();
+
 
+
protected:
+
    // Overridden virtual functions:
+
    virtual streamsize showmanyc();
+
    virtual int_type underflow();
+
    virtual int_type uflow();
+
    virtual int_type pbackfail(int_type c = traits::eof());
+
    virtual int_type overflow (int_type c = traits::eof());
+
    virtual basic_streambuf<charT,traits>* setbuf(char_type* s, streamsize n);
+
    virtual pos_type seekoff(off_type off, ios_base::seekdir way,
+
                            ios_base::openmode which = ios_base::in{{!}}ios_base::out);
+
    virtual pos_type seekpos(pos_type sp,
+
                            ios_base::openmode which = ios_base::in{{!}}ios_base::out);
+
    virtual int sync();
+
    virtual void imbue(const locale& loc);
+
};
+
}}
+
 
+
====Class {{lc|std::basic_ifstream}}====
+
{{source|1=
+
template <class charT, class traits = char_traits<charT> >
+
class basic_ifstream : public basic_istream<charT,traits> {
+
public:
+
    typedef charT char_type;
+
    typedef typename traits::int_type int_type;
+
    typedef typename traits::pos_type pos_type;
+
    typedef typename traits::off_type off_type;
+
    typedef traits traits_type;
+
 
+
    // Constructors:
+
    basic_ifstream();
+
    explicit basic_ifstream(const char* s, ios_base::openmode mode = ios_base::in);
+
    explicit basic_ifstream(const string& s, ios_base::openmode mode = ios_base::in);
+
    basic_ifstream(const basic_ifstream& rhs) = delete;
+
    basic_ifstream(basic_ifstream&& rhs);
+
 
+
    // Assign/swap:
+
    basic_ifstream& operator=(const basic_ifstream& rhs) = delete;
+
    basic_ifstream& operator=(basic_ifstream&& rhs);
+
    void swap(basic_ifstream& rhs);
+
 
+
    // Members:
+
    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 string& s, ios_base::openmode mode = ios_base::in);
+
    void close();
+
 
+
private:
+
    basic_filebuf<charT,traits> sb; // exposition only
+
};
+
}}
+
 
+
====Class {{lc|std::basic_ofstream}}====
+
{{source|1=
+
template <class charT, class traits = char_traits<charT> >
+
class basic_ofstream : public basic_ostream<charT,traits> {
+
public:
+
    typedef charT char_type;
+
    typedef typename traits::int_type int_type;
+
    typedef typename traits::pos_type pos_type;
+
    typedef typename traits::off_type off_type;
+
    typedef traits traits_type;
+
 
+
    // Constructors:
+
    basic_ofstream();
+
    explicit basic_ofstream(const char* s, ios_base::openmode mode = ios_base::out);
+
    explicit basic_ofstream(const string& s, ios_base::openmode mode = ios_base::out);
+
    basic_ofstream(const basic_ofstream& rhs) = delete;
+
    basic_ofstream(basic_ofstream&& rhs);
+
 
+
    // Assign/swap:
+
    basic_ofstream& operator=(const basic_ofstream& rhs) = delete;
+
    basic_ofstream& operator=(basic_ofstream&& rhs);
+
    void swap(basic_ofstream& rhs);
+
 
+
    // Members:
+
    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 string& s, ios_base::openmode mode = ios_base::out);
+
    void close();
+
 
+
private:
+
    basic_filebuf<charT,traits> sb; // exposition only
+
};
+
}}
+
 
+
====Class {{lc|std::basic_fstream}}====
+
{{source|1=
+
template <class charT, class traits=char_traits<charT> >
+
class basic_fstream : public basic_iostream<charT,traits> {
+
public:
+
    typedef charT char_type;
+
    typedef typename traits::int_type int_type;
+
    typedef typename traits::pos_type pos_type;
+
    typedef typename traits::off_type off_type;
+
    typedef traits traits_type;
+
 
+
    // constructors/destructor
+
    basic_fstream();
+
    explicit basic_fstream(const char* 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);
+
    basic_fstream(const basic_fstream& rhs) = delete;
+
    basic_fstream(basic_fstream&& rhs);
+
 
+
    // Assign/swap:
+
    basic_fstream& operator=(const basic_fstream& rhs) = delete;
+
    basic_fstream& operator=(basic_fstream&& rhs);
+
    void swap(basic_fstream& rhs);
+
 
+
    // Members:
+
    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 string& s,
+
              ios_base::openmode mode = ios_base::in{{!}}ios_base::out);
+
    void close();
+
 
+
private:
+
    basic_filebuf<charT,traits> sb; // exposition only
+
};
+
}}
+
 
+
{{langlinks|zh}}
+

Latest revision as of 15:02, 25 October 2024

 
 
Standard library headers
 

This header is part of the Input/Output library.

Contents

Classes

implements raw file device
(class template) [edit]
implements high-level file stream input operations
(class template) [edit]
implements high-level file stream output operations
(class template) [edit]
implements high-level file stream input/output operations
(class template) [edit]
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)

Functions

specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]
specializes the std::swap algorithm
(function template) [edit]

[edit] Synopsis

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>;
}

[edit] Class template 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;
 
    // constructors/destructor
    basic_filebuf();
    basic_filebuf(const basic_filebuf&) = delete;
    basic_filebuf(basic_filebuf&& rhs);
    virtual ~basic_filebuf();
 
    // assign and swap
    basic_filebuf& operator=(const basic_filebuf&) = delete;
    basic_filebuf& operator=(basic_filebuf&& rhs);
    void swap(basic_filebuf& rhs);
 
    // members
    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);   // wide systems only
    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:
    // overridden virtual functions
    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);
}

[edit] Class template 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;
 
    // constructors
    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);// wide systems only
    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);
 
    // assign and swap
    basic_ifstream& operator=(const basic_ifstream&) = delete;
    basic_ifstream& operator=(basic_ifstream&& rhs);
    void swap(basic_ifstream& rhs);
 
    // members
    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);  // wide systems only
    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;    // exposition only
  };
 
  template<class CharT, class Traits>
    void swap(basic_ifstream<CharT, Traits>& x,
              basic_ifstream<CharT, Traits>& y);
}

[edit] Class template 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;
 
    // constructors
    basic_ofstream();
    explicit basic_ofstream(const char* s,
                            ios_base::openmode mode = ios_base::out);
    explicit basic_ofstream(const filesystem::path::value_type* s,  // wide systems only
                            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);
 
    // assign and swap
    basic_ofstream& operator=(const basic_ofstream&) = delete;
    basic_ofstream& operator=(basic_ofstream&& rhs);
    void swap(basic_ofstream& rhs);
 
    // members
    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);     // wide systems only
    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;    // exposition only
  };
 
  template<class CharT, class Traits>
    void swap(basic_ofstream<CharT, Traits>& x,
              basic_ofstream<CharT, Traits>& y);
}

[edit] Class template 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;
 
    // constructors
    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);    // wide systems only
    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);
 
    // assign and swap
    basic_fstream& operator=(const basic_fstream&) = delete;
    basic_fstream& operator=(basic_fstream&& rhs);
    void swap(basic_fstream& rhs);
 
    // members
    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);    // wide systems only
    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;    // exposition only
  };
 
  template<class CharT, class Traits>
    void swap(basic_fstream<CharT, Traits>& x,
              basic_fstream<CharT, Traits>& y);
}