- QDir *dir = new QDir;
- QDateTime currentTime = QDateTime::currentDateTime();
- QString logName = "log_" + currentTime.toString("yyyyMMddhhmmss");
-
- bool exist = dir.exists(QDir::currentPath().append("/log"));
-
- if(!exist)
- {
- dir.mkpath(QDir::currentPath().append("/log"));
- dir.setPath(QDir::currentPath().append("/log"));
- }
-
- QFile *file = new QFile(logName + ".txt");
- dir.setCurrent(QDir::currentPath().append("/log"));
- while (!file.open(QIODevice::WriteOnly | QIODevice::append));
- {
- QTextStream txtOutput(file);
- txtOutput << str << endl;
- file.close();
- }
bool exist = dir.exists(QDir::currentPath().append("/log"));
if(!exist)
{
dir.mkpath(QDir::currentPath().append("/log"));
dir.setPath(QDir::currentPath().append("/log"));
}
QFile *file = new QFile(logName + “.txt”);
dir.setCurrent(QDir::currentPath().append("/log"));
while (!file.open(QIODevice::WriteOnly | QIODevice::append));
{
QTextStream txtOutput(file);
txtOutput << str << endl;
file.close();
}
本文介绍了如何使用C++的QDir和QFile类来创建并写入带有时间戳的日志文件,包括路径创建、文件打开和文本输出。重点在于文件系统操作的实践和文件管理在软件开发中的应用。
2190

被折叠的 条评论
为什么被折叠?



