我正在编写一个游戏并创建了一个地图加载器和日志。加载一个块只能有效一次,当再次加载时一切都会崩溃。代码:
logout << "[CORE]: [LOAD]: [LOADCLUSTER]: <info>: in_time: [" << (GetTime().c_str()) << "]: Entered in LoadCluster function" << '\n';
ifstream in_data;
string nameclst = "files/worldinfo/cluster_" + to_string(x) + "_" + to_string(y) + ".clst";
logout << "[CORE]: [LOAD]: [LOADCLUSTER]: <info>: in_time: [" << (GetTime().c_str()) << "]: Loading data from: " << nameclst << '\n';
in_data.open(nameclst, ios::in);
cout << "reading information from cluster: " << nameclst << "\n";
string in_dstr = "";
loadedCluster = {x, y};
unsigned int line = 0;
while(1)
{
getline(in_data, in_dstr);
if(in_dstr == "end")
break;
if(in_dstr == "start")
getline(in_data, in_dstr); line++;
if(in_dstr == " "){
logout << "[CORE]: [LOAD]: [LOADCLUSTER]: <error>: in_time: [" << (GetTime().c_str()) << "]: File: " << nameclst << " breaked or have no information" << '\n';
MessageBox(NULL, (string("Cannot load information from cluster: ") + (nameclst)).c_str(), "Engine error", MB_ICONWARNING | MB_OK);
return 1;
break;
}
//обработка строки
line++;
_sleep(1);
}
日志通常都很糟糕。如果通过 IDE 运行它,它会读取并重写所有日志,但如果在没有 IDE 的情况下通过 exe 运行它,则在进入该函数时一切都会崩溃。代码:
void CreateNewLog()
{
string witinglogs = "";
witinglogs += "[SYSTEM]: <info>: in_time: [" + string(GetTime().c_str()) + "]: CORE started" + '\n';
witinglogs += "[CORE]: [LOGS]: <info>: in_time: [" + string(GetTime().c_str()) + "]: Mathing logs" + '\n';
ifstream logCount("logs/logCount.math");
logout.close();
ifstream login("logs/latestLog.log");
string logs;
getline(logCount, logs);
int logsc = stoi(logs);
logCount.close();
ofstream logCounte("logs/logCount.math");
logCounte << logsc + 1;
witinglogs += "[CORE]: [LOGS]: <info>: in_time: [" + string(GetTime().c_str()) + "]: Mathed " + to_string(logsc) + " logs" + "\n";
string logtr = "logs/Log#" + to_string(logsc) + ".log";
ofstream logOld(logtr);
string readedLog = "";
getline(login, readedLog);
witinglogs += "[CORE]: [LOGS]: <info>: in_time: [" + string(GetTime().c_str()) + "]: Rewriting logs" + "\n";
int lines = 0;
while(readedLog != "[QUIT]")
{
lines++;
if(readedLog == "[QUIT]" || lines >= 100000)
break;
logOld << readedLog << '\n';
getline(login, readedLog);
}
logOld << "[QUIT]";
witinglogs += "[CORE]: [LOGS]: <info>: in_time: [" + string(GetTime().c_str()) + "]: Rewrited " + to_string(lines) + " lines" + "\n";
logOld.close();
login.close();
logout.open("logs\\latestLog.log", ios_base::trunc);
logout << witinglogs;
}
我使用 IDE Code::Blocs。
我设法改正了错误。我只是将所有声明移到单独的函数
ifstream
中ofstream
,而不是读取和重写文件,我只是使用std::filesystem::copy()
。据我了解,如果同时声明两个线程,它们可以访问已经占用的内存。感谢所有试图帮助我解决问题的人。感谢你们让我没有放弃,继续努力,并取得了成果。