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

defined in the header file <fstream>

template<

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

> class basic_fstream : public std::basic_iostream<CharT, Traits>

 Class Template basic_fstreamImplements high-level input/output over file-based streams. It gives the high-level interface of std::basic_iostream to file-based buffering ( std::basic_filebuf ).

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

 Also defines two specializations for common character types:

type definition
fstream basic_fstream<char>
wfstream basic_fstream<wchar_t>


file operation

Checks if a stream has an associated file

std::basic_fstream<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::fstream fstream1("test1.txt", std::ios::in | std::ios::out);
    std::fstream fstream2("test2.txt", std::ios::in | std::ios::out);
    std::fstream fstream3("test3.txt", std::ios::in | std::ios::out);

    std::cout << "fstream1 is: "
              << (fstream1.is_open() ? "true" : "false") << std::endl;
    std::cout << "fstream2 is: "
              << (fstream2.is_open() ? "true" : "false") << std::endl;
    std::cout << "fstream3 is: "
              << (fstream3.is_open() ? "true" : "false") << std::endl;

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

output

 

open the file and associate it with the stream

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

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

(1)

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

(2) (since C++17)

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

(3) (since C++11)

void open( const std::filesystem::path &filename,                                  
           ios_base::openmode mode = ios_base::in|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). (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::fstream fstream1;
    //1-2) 等效地调用 rdbuf()->open(filename, mode ).
    fstream1.open(strFileName1.c_str(), std::ios::in | std::ios::out);

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

    std::fstream fstream3;
    std::string strFileName3 = "test3.txt";
    fstream2.open(strFileName3, std::ios::in | std::ios::out);

    std::cout << "fstream1 is: "
              << (fstream1.is_open() ? "true" : "false") << std::endl;
    std::cout << "fstream2 is: "
              << (fstream2.is_open() ? "true" : "false") << std::endl;
    std::cout << "fstream3 is: "
              << (fstream3.is_open() ? "true" : "false") << std::endl;

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

output

 

Guess you like

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