如何在不重新创建现有文件的情况下写入.txt文件

时间:2018-08-16 11:20:16

标签: c++ fstream

我有基本的日志记录过程。如果程序中发生错误,则必须将其记录到.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
  

{“日志”:“传入   Message {\“ Action \”:\“ Heartbeat \”,\“ Data \”:null}“,” Process“:” WebSocket“,” Time“:” 2018-08-16.14:53:52“}      

{“日志”:“ GSM设置   已完成“,”流程“:” SMSService“,”时间“:” 2018-08-16.14:54:13“}

  • 使用CTRL-C停止程序并控制state_logs.txt,现在我可以在其中看到两行了。
  • 重新启动程序,并在10秒钟内再次用CTRC-C中断(在记录新行之前)。
  • 我再次检查state_logs.txt,现在什么也看不到。重新执行此过程,但是这次在中断程序之前等待了更多时间(再等待一小段时间即可得到一行日志。)。所以现在我只能看到一个,并且时间戳已更改。

1 个答案:

答案 0 :(得分:4)

我无法复制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
}

测试会话:

$ 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()有效(进入系统)有多远。 (在日常工作中,我尝试以不必依赖此类细节的方式编写代码...);-)

相关问题