一.效果
如下图所示,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_fileclass 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();