Есть файл:
const std::string logFile = "log.txt";
Есть путь к нему:
const std::string logPath = "Logs/tmp/"
Как создать папки по пути к файлу, если из нет.
А также как создать файл, если его нет, а если есть - дописать в его конец.
Также, поскольку я использую C++11, я не могу использовать std::filesystem
или std::experimental
.
У меня сейчас так:
#ifndef DEBUGGER_HPP
#define DEBUGGER_HPP
#include <ctime>
#include <fstream>
#include <iostream>
#include "../Main/Settings.hpp"
namespace Debugger
{
//
void PrintDebug(const std::string input){
if (Settings::settings_DebugMode >= 2){
// Add current datetime
auto seconds = time(NULL);
auto* timeinfo = localtime(&seconds);
std::string result = "[DEBUG] [ ";
result += asctime(timeinfo);
result.erase(result.rfind('\n'), 1);
result += " ] " + input + '\n';
if (Settings::settings_LogPath == "console"){
std::cout << result;
} else {
// Create folder if it doesn't exist
for(std::string::size_type pos = 0; (pos = Settings::settings_LogPath.find('/',pos+1)) != std::string::npos;)
{
mkdir(Settings::settings_LogPath.substr(0,pos).c_str());
}
// Create logFile
std::ofstream logFile;
logFile.open("log.txt", std::ios::app);
logFile << result;
logFile.close();
}
}
}
}
#endif // DEBUGGER_HPP
QFile::exists
<filesystem>