c++11 standard template (STL) (std::basic_ofstream) (4)

defined in the header file <fstream>

template<

    class CharT,
    class Traits = std::char_traits<CharT>

> class basic_ofstream : public std::basic_ostream<CharT, Traits>

 Class Template basic_ofstreamImplements high-level stream-based output operations on files. It gives the high-level interface of std::basic_ostream to a file-based stream buffer ( std::basic_filebuf ).

std::basic_ofstreamA typical implementation keeps only one non-exported member: an instance of std::basic_filebuf<CharT, Traits> .

 Two specializations are also defined for common character types:

type definition
ofstream basic_ofstream<char>
wofstream basic_ofstream<wchar_t>


file operation

Checks if a stream has an associated file

std::basic_ofstream<CharT,Traits>::is_open

bool is_open();

(until C++11)

bool is_open() const;

(since C++11)

 Checks if a file stream has an associated file.

Equivalently calls rdbuf()->is_open().

parameter

(none)

return value

true if the file stream has an associated file, false otherwise.

call example

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

int main()
{
    std::ofstream ofstream1("test1.txt", std::ios::in);
    std::ofstream ofstream2("test2.txt", std::ios::in);
    std::ofstream ofstream3("test3.txt", std::ios::in);
    std::cout << "ofstream1 is: "
              << (ofstream1.is_open() ? "true" : "false") << std::endl;
    std::cout << "ofstream2 is: "
              << (ofstream2.is_open() ? "true" : "false") << std::endl;
    std::cout << "ofstream3 is: "
              << (ofstream3.is_open() ? "true" : "false") << std::endl;

    std::cout << std::endl;
    return 0;
}

output

open the file and associate it with the stream

std::basic_ofstream<CharT,Traits>::open

void open( const char *filename,
           ios_base::openmode mode = ios_base::out );

(1)

void open( const std::filesystem::path::value_type *filename,
           ios_base::openmode mode = ios_base::out );

(2) (since C++17)

void open( const std::string &filename,                                  
           ios_base::openmode mode = ios_base::out );

(3) (since C++11)

void open( const std::filesystem::path &filename,                                  
           ios_base::openmode mode = ios_base::out );

(4) (since C++17)

Opens the file named filenameand associates it with a file stream.

Call setstate(failbit) on failure.

Call clear() on success. (since C++11)

1-2) Equivalently calls rdbuf()->open(filename, mode | ios_base::out). (See std::basic_filebuf::open for details on the effect of this call). Overload (2) is provided only if std::filesystem::path::value_type is not char. (since C++17)

3-4) Equivalently calls (1-2) as if with open(filename.c_str(), mode).

parameter

filename - filename to open
mode - Specifies the open mode. It is a bitmask type and defines the following constants:
constant explain
app Seek to end of stream before each write
binary open in binary mode
in open for reading
out open for writing
trunk Discards the contents of the stream on open
ate Seeks to end of stream immediately after opening

return value

(none)

call example 

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

int main()
{
    std::string strFileName1 = "test1.txt";
    std::ofstream ofstream1;
    //1-2) 等效地调用 rdbuf()->open(filename, mode | ios_base::out).
    ofstream1.open(strFileName1.c_str(), std::ios::out);

    std::ofstream ofstream2;
    std::string strFileName2 = "test2.txt";
    //3-4) 等效地调用 (1-2) ,如同以 open(filename.c_str(), mode) 。
    ofstream2.open(strFileName2, std::ios::out);

    std::ofstream ofstream3;
    std::string strFileName3 = "test3.txt";
    ofstream2.open(strFileName3, std::ios::out);

    std::cout << "ofstream1 is: "
              << (ofstream1.is_open() ? "true" : "false") << std::endl;
    std::cout << "ofstream2 is: "
              << (ofstream2.is_open() ? "true" : "false") << std::endl;
    std::cout << "ofstream3 is: "
              << (ofstream3.is_open() ? "true" : "false") << std::endl;

    std::cout << std::endl;
    return 0;
}

output

Guess you like

Origin blog.csdn.net/qq_40788199/article/details/132118444