defined in the header file <fstream>
template< class CharT, |
Class Template basic_ofstream
Implements 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_ofstream
Typical implementations keep 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
close associated file
std::basic_ofstream<CharT,Traits>::close
void close(); |
Close the associated file.
Equivalently calls rdbuf()->close(). If an error occurred during the operation, setstate(failbit) is called.
parameter
(none)
return value
(none)
Notice
This function is called as the destructor of basic_ofstream when the stream object leaves the scope, usually not called directly.
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;
std::cout << "std::ofstream close" << std::endl;
ofstream1.close();
ofstream2.close();
ofstream3.close();
std::cout << std::endl;
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
non-member function
specialization of the std::swap algorithm
std::swap(std::basic_ofstream)
template< class CharT, class Traits > |
Specializes the std::swap algorithm for std::basic_ofstream. Swap the state of lhs
and rhs
. Equivalently call lhs.swap(rhs).
parameter
lhs, rhs | - | stream to exchange state |
return value
(none)
abnormal
(none)
call example
#include <fstream>
#include <utility>
#include <string>
#include <iostream>
int main()
{
std::ofstream ofstream1("test1.txt", std::ios::out);
std::cout << "ofstream1 is: "
<< (ofstream1 ? "true" : "false") << std::endl;
std::ofstream ofstream2("test2.txt", std::ios::out);
std::cout << "ofstream2 is: "
<< (ofstream2 ? "true" : "false") << std::endl;
std::cout << std::endl;
ofstream1 << "hello 1" << " ";
ofstream2 << "hello 2" << " ";
//为 std::basic_ofstream 特化 std::swap 算法。
//交换 lhs 与 rhs 的状态。等效地调用 lhs.swap(rhs) 。
std::cout << "std::swap(ofstream1, ofstream2) " << std::endl;
std::swap(ofstream1, ofstream2);
ofstream1 << "hello 1" << " ";
ofstream2 << "hello 2" << " ";
ofstream1.close();
ofstream2.close();
std::cout << std::endl;
std::ifstream ifstream1("test1.txt", std::ios::in);
std::cout << "ifstream1 is: "
<< (ifstream1.is_open() ? "true" : "false") << std::endl;
if (ifstream1.is_open())
{
std::cout << ifstream1.rdbuf() << std::endl;
}
std::cout << std::endl;
std::ifstream ifstream2("test2.txt", std::ios::in);
std::cout << "ifstream2 is: "
<< (ifstream2.is_open() ? "true" : "false") << std::endl;
if (ifstream2.is_open())
{
std::cout << ifstream2.rdbuf() << std::endl;
}
std::cout << std::endl;
return 0;
}
output