如下图所示,test.log是我正在写入的文件。
#include <fstream>
#include <io.h>
#include <iostream>
#include <stdio.h>
class OfSteamImmediately : public std::ofstream
{
public:
OfSteamImmediately()
{
}
~OfSteamImmediately()
{
if (std::ofstream::is_open())
{
std::ofstream::close();
}
}
void open(const char *path, ios_base::openmode mode = ios_base::out)
{
// 访问protected变量_M_file
class MyFileBuf : public std::filebuf
{
public:
FILE *file()
{
return _M_file.file();
}
};
std::ofstream::open(path, mode);
auto fb = std::ofstream::rdbuf();
FILE *fp = ((MyFileBuf *)fb)->file();