如何将控制台输出中的所有内容复制到文本文件?

How do you copy everything from the console's output to a text file?

本文关键字:复制 文本 文件 控制台 输出      更新时间:2023-10-16

我已经尝试过Sink和SaveHistory来做到这一点,但没有一个奏效。我还尝试了诸如其中一个答案中的这个问题中的代码之类的东西。如何在 C++/Windows 中输出到控制台。我在Visual Studio 2019中的一个空解决方案中制作了所有内容。

没有任何错误,但是当我这样做时:

ofstream myfile;
myfile.open("Addresses.txt");
myfile << printf << endl;
myfile.close();
return 0;

它打印出随机数。

//Code I tried
#include <iostream>
using namespace std;
int main (int) {
cout << "This will print to the console!" << endl; // I don't know how to make it read all of the output
}

我的目标是将控制台的整个输出放在我选择的文本文件中。(地址.txt(

当您调用myfile << printf << endl时,您不会从控制台获取文本,而是获取函数的标识符。要从控制台获取文本,请查看 std::cin ex。

int x;
std::cin >> x;
std::cout << "You Entered: " << x << std::endl;

旁注,请避免使用using namespace这使得代码更易于阅读,因为它清楚地表明它是您的函数还是其他库