Namespaces
Variants
Views
Actions

Difference between revisions of "Template:cpp/io/basic fstream/constructor"

From cppreference.com
m (Use since= and until= params of {{ddcl}} template.)
(Trying to hack around the layout bug...)
Line 57: Line 57:
 
int main()
 
int main()
 
{
 
{
{{#switch: {{{1|}}}
+
    {{#switch: {{{1|}}}
  | basic_ifstream=   std::ifstream f0;
+
  | basic_ifstream=std::ifstream f0;
 
     std::ifstream f1("test.bin", std::ios::binary);
 
     std::ifstream f1("test.bin", std::ios::binary);
 
     std::string name = "example.txt";
 
     std::string name = "example.txt";
 
     std::ifstream f2(name);
 
     std::ifstream f2(name);
 
     std::ifstream f3(std::move(f1));
 
     std::ifstream f3(std::move(f1));
  | basic_ofstream=   std::{{{1|}}} f0;
+
  | basic_ofstream=std::{{{1|}}} f0;
 
     std::ofstream f1("test.bin", std::ios::binary);
 
     std::ofstream f1("test.bin", std::ios::binary);
 
     std::string name = "example.txt";
 
     std::string name = "example.txt";
 
     std::ofstream f2(name);
 
     std::ofstream f2(name);
 
     std::ofstream f3(std::move(f1));
 
     std::ofstream f3(std::move(f1));
  | basic_fstream=   std::fstream f0;
+
  | basic_fstream=std::fstream f0;
 
     std::fstream f1("test.bin", std::ios::binary);
 
     std::fstream f1("test.bin", std::ios::binary);
 
     std::string name = "example.txt";
 
     std::string name = "example.txt";

Revision as of 12:59, 22 October 2014


{{{1}}}();
(1)
explicit {{{1}}}( const char* filename,
         ios_base::openmode mode =
(2)
explicit {{{1}}}( const string& filename,                                  
         ios_base::openmode mode =
(3) (since C++11)
{{{1}}}( {{{1}}}&& other );
(4) (since C++11)
{{{1}}}( const {{{1}}}& rhs) = delete;
(5) (since C++11)

Constructs new file stream.

1) Default constructor: constructs a stream that is not associated with a file: default-constructs the std::basic_filebuf and constructs the base with the pointer to this default-constructed std::basic_filebuf member.
2) First, performs the same steps as the default constructor, then asssociates the stream with a file by calling . If the open() call returns a null pointer, sets setstate(failbit).
3) Same as (filename.c_str(), mode).
4) Move constructor. First, move-constructs the base class from other (which does not affect the rdbuf() pointer), then move-constructs the std::basic_filebuf member, then calls this->set_rdbuf() to install the new basic_filebuf as the rdbuf() pointer in the base class.
5) The copy-constructor is deleted: this class is not copyable.

Parameters

filename - the name of the file to be opened
mode - specifies stream open mode. It is bitmask type, the following constants are defined:
Constant Explanation
app seek to the end of stream before each write
binary open in binary mode
in open for reading
out open for writing
trunc discard the contents of the stream when opening
ate seek to the end of stream immediately after open
noreplace (C++23) open in exclusive mode
other - another file stream to use as source

Example

#include <fstream>
#include <utility>
#include <string>
int main()
{
 
}

See also

opens a file and associates it with the stream
(public member function of std::{{{1}}}) [edit]
opens a file and configures it as the associated character sequence
(public member function of std::basic_filebuf<CharT,Traits>) [edit]
replaces the rdbuf without clearing its error state
(protected member function) [edit]