【问题标题】:Log the Command Line in C++在 C++ 中记录命令行
【发布时间】:2017-09-08 22:22:26
【问题描述】:

我正在尝试制作一个使用命令行来显示数据的脚本,并且我希望能够将输出到命令行的所有内容记录到一个单独的文件中。

我正在使用 fstream、iostream 和 std 命名空间。我只需要知道如何引用命令行 CmdExample.exe 以及它所说的将其写入 txt 文件的所有内容。

例子:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    cout << "This is some text I want to reference when the program ends" << endl << "and write to a txt file.";

    return 0;
}

【问题讨论】:

标签: c++ logging


【解决方案1】:

您可以使用输出重定向。即:

./a.out > my_output.txt

这会将本应输出到终端的所有内容放入一个新文件my_output.txt(或覆盖文件中已有的所有内容)。

如果你也想要cerr输出的东西,你可以修改为:

./a.out 2>&1 > my_output.txt #push stderr -> stdout, then stdout -> my_output.txt

同样,您也可以使用script。要使用脚本,您可以:

script my_output.txt #start the script to my_output.txt 
./a.out #run program
exit #end script

【讨论】:

  • 我想我应该开始在 linux 环境中编码。
  • @Jones Crimson 有一些适用于 Windows 的 bash 模拟器,你可以在其中工作,效果很好。
猜你喜欢
  • 1970-01-01
  • 2011-04-13
  • 2011-11-09
  • 1970-01-01
  • 2020-05-22
  • 2017-01-30
  • 1970-01-01
  • 2021-10-28
  • 2014-05-11
相关资源
最近更新 更多