std::cout的文件输出
c++中想要用std::cout这种控制台输出内容操作,输出到指定文件
.h文件(a.h)
#ifndef _A_H
#define _A_H
#include <fstream>
#include <iostream>
#include <string>
class A
{
private:
std::ofstream* outstream = NULL;
public:
A();
~A();
A& operator<<(const std::string& std);
A& operator<<(const int value);
A& operator<<(std::ostream& (*os)(std::ostream&));
};
#endif
.cpp文件(a.cpp)
#include "a.h"
#include <iostream>
#include <string>
#define FILE_NAME "a.log"
A::A() {
outstream = new std::ofstream(FILE_NAME);
}
A::~A() {
outstream->close();
delete outstream;
}
A& A::operator<<(const std::string& str) {
(*outstream) << str;
return (*this);
}
A& A::operator<<(const int value) {
(*outstream) << value;
return (*this);
}
A& A::operator<<(std::ostream& (*os)(std::ostream&)) {
(*outstream) << os;
return (*this);
}
main使用
#include <iostream>
#include <string.h>
#include "a.h"
A* a= new A();
int main(int argc, char* argv[])
{
(*decompileLogger) << "\t(" << 12 << ")" << std::endl;
}
最后博主简简单单使用工具