我有基本的记录过程。当程序中发生错误时,它必须记录到一个 .txt 文件中。我为此使用以下代码:
#include <fstream>
fileName = "logs/error_log.txt";
ofstream myfile;
myfile.open (fileName,fstream::app);
myfile << serialized_string << endl;
myfile.close();
当发生错误时,它会成功进入 error_log.txt 文件。但是当程序崩溃并在之后重新启动时,新日志不会记录为追加。正如预期的那样,我使用的方式创建了一个具有相同名称的新文件,并在其上写入。有人可以解释一下我应该如何编写较旧的日志吗?
编辑:这些是我面临的步骤: 我正在使用 raspbian 并使用以下内容进行编译:
g++ main.cpp -lwiringPi -lpthread -lcurl -o test
这就是整个函数。
int putLog(const char* process, int logType, string logData) {
isLoggerBusy = true;
string fileName;
std::string color;
switch (logType) {
case 0:
fileName = "logs/error_log.txt";
// color = "\033[0;31m";
break;
case 1:
fileName = "logs/info_log.txt";
// color = "\033[0;36m";
break;
case 2:
fileName = "logs/state_log.txt";
// color = "\033[1;33m";
break;
}
if (process == "WebSocket") {
color = "\033[1;32m";
}
json j = {
{"Process", process}, {"Time", currentDateTime()}, {"Log", logData}};
string serialized_string = j.dump();
fix_utf8_string(serialized_string);
ofstream myfile;
myfile.open(fileName, fstream::app);
cout << color << serialized_string << '\n';
myfile << serialized_string << endl;
myfile.close();
isLoggerBusy = false;
cout << "\033[0m" << endl;
return 0;
}
- 我启动了程序。它将这些行写到 state_logs.txt
{"Log":"Incoming Message{\"Action\":\"Heartbeat\",\"Data\":null}","Process":"WebSocket","Time":"2018-08-16.14:53:52"}
{"Log":"GSM Setup Finished","Process":"SMSService","Time":"2018-08-16.14:54:13"}
- 使用 CTRL-C 停止程序并控制 state_logs.txt,我现在可以看到那里有两行。
- 重新启动程序并在 10 秒内再次使用 CTRC-C 中断(在新行记录之前。)
- 我再次检查了 state_logs.txt,现在什么也看不到了。重新执行此过程,但这次在中断程序之前等待多一点(只多等待一行日志。)。所以现在我只能看到一个并且时间戳已更改。
最佳答案
我无法重现 OP 描述的内容。
我刚刚在 cygwin 上测试过/Windows 10。(我不知道如何在在线编译器上进行此测试。)
testFStreamApp.cc
:
#include <iostream>
#include <fstream>
int main()
{
std::cout << "Log error...\n";
{ std::ofstream log("testFStream.log", std::ios::out | std::ios::app);
log << "Error happened!" << std::endl;
}
std::cout << "Going to die...\n";
abort();
return 0; // should never be reached
}
测试 session :
$ g++ -std=c++11 -o testFStreamApp testFStreamApp.cc
$ rm testFStream.log
$ for i in 1 2 3; do
> echo "$i. start:"
> ./testFStreamApp
> done
1. start:
Log error...
Going to die...
Aborted (core dumped)
2. start:
Log error...
Going to die...
Aborted (core dumped)
3. start:
Log error...
Going to die...
Aborted (core dumped)
$ cat <testFStream.log
Error happened!
Error happened!
Error happened!
$
YSC 指出我做了一些默默的改变。我假设没有任何相关性。
然而,为了消除任何借口,我也尝试过:
#include <iostream>
#include <fstream>
int main()
{
std::cout << "Log error...\n";
std::ofstream log;
log.open("testFStream.log", std::fstream::app);
log << "Error happened!" << std::endl;
log.close();
std::cout << "Going to die...\n";
abort();
return 0; // should never be reached
}
输出与上面完全一样。
我不敢测试这个,但 doctorlove 鼓励我:
#include <iostream>
#include <fstream>
int main()
{
std::cout << "Log error...\n";
std::ofstream log;
log.open("testFStream.log", std::fstream::app);
log << "Error happened!" << std::endl;
std::cout << "Going to die...\n";
abort();
log.close();
return 0; // should never be reached
}
即使在这种情况下,我也得到了相同的结果。
在这一点上,我必须承认 cygwin 只是 win32 API 的包装器。因此,在这种情况下,我不会怀疑这在其他操作系统上的行为是否有所不同。
我知道 std::endl
执行 flush()
洞察。问题是 flush()
有多深(进入系统)是有效的。 (在日常工作中,我尽量以一种不需要依赖这些细节的方式编写代码......);-)
关于c++ - 如何在不重新创建现有文件的情况下写入 .txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51875820/