windows添加debug信息,并存储txt文件
.h
#ifndef FILE_UTIL_H
#define FILE_UTIL_H
#include <string>
void donghaodebug(const std::string& content);
#endif // FILE_UTIL_H
.cpp
#include "donghaodebug.h"
#include <fstream>
#include<ctime>
void donghaodebug(const std::string& content) {
std::string filename="./donghaodebug.txt";
std::ofstream file(filename, std::ios::app);
static bool firstcall=true;
if (file.is_open()) {
if(firstcall)
{
firstcall=false;
time_t currentTime = time(nullptr);
struct tm* localTime = localtime(¤tTime);
char timeStr[20];
strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", localTime);
file << "-------------------------新debug信息-----------------------------" << std::endl;
file << "时间: " << timeStr << std::endl;
}
file << content << std::endl;
file.close();
} else {
std::ofstream newFile(filename);
newFile << content << std::endl;
newFile.close();
}
}
本文来自博客园,作者:东岸,转载请注明原文链接:https://www.cnblogs.com/donghao99/p/18217807