将所有程序输出写入 C++ 中的 txt 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/574543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:02:03  来源:igfitidea点击:

Writing ALL program output to a txt file in C++

c++file-io

提问by user40120

I need to write all my program output to a text file. I believe it's done this way,

我需要将所有程序输出写入文本文件。我相信它是这样完成的,

sOutFile << stdout;

where sOutFile is the ofstream object that creates the file like this:

其中 sOutFile 是创建文件的 ofstream 对象,如下所示:

sOutFile("CreateAFile.txt" ); // CreateAFile.txt is created.

When I insert the stdout into the sOutFile object, I get some code which seems to resemble octal[hexadecimal] code or an address of some kind in the text file that I created.

当我将标准输出插入到 sOutFile 对象中时,我得到了一些似乎类似于 八进制[十六进制] 代码或我创建的文本文件中的某种地址。

0x77c5fca0

But what's confusing to me is that in my program I use cout several times. Mostly just literal statement. If I'm not mistaken that is the program output.

但令我困惑的是,在我的程序中,我多次使用 cout。大多只是字面上的陈述。如果我没记错的话,那是程序输出。

If this code is an address, would it contain all of my output? Could I read it back in to the program and find out that way?

如果此代码是一个地址,它会包含我的所有输出吗?我可以将它读回程序并以这种方式找到吗?

What can I do to get ALL of my program output written to a text file?

我该怎么做才能将所有程序输出写入文本文件?

回答by Andrew Coleson

If your program already uses cout/printf and you want to send EVERYTHING you currently output to a file, you could simply redirect stdout to point to a file before your existing calls: http://support.microsoft.com/kb/58667

如果您的程序已经使用 cout/printf 并且您想将当前输出的所有内容发送到文件,您可以简单地将 stdout 重定向到现有调用之前的文件:http: //support.microsoft.com/kb/58667

Relevant Code:

相关代码:

freopen( "file.txt", "w", stdout );
cout << "hello file world\n"; // goes to file.txt
freopen("CON", "w", stdout);
printf("Hello again, console\n"); // redirected back to the console

Alternatively if you just want Some things to be printed to a file, you just want a regular file output stream: http://www.cplusplus.com/doc/tutorial/files.html

或者,如果您只想将某些内容打印到文件中,您只需要一个常规的文件输出流:http: //www.cplusplus.com/doc/tutorial/files.html

Relevant Code:

相关代码:

ofstream myfile;
myfile.open("file.txt");
myfile << "Hello file world.\n";
printf("Hello console.\n");
myfile.close();

EDIT to aggregate answers from John T and Brian Bondy:
Finally, if you're running it from the commandline, you can just redirect the output as everyone else mentioned by using the redirect operator ">" or append ">>":

编辑以汇总 John T 和 Brian Bondy 的答案:
最后,如果您从命令行运行它,您可以使用重定向运算符 ">" 或附加 ">>" 将输出重定向为其他人提到的:

myProg > stdout.txt 2> stderr.txt

回答by Brian R. Bondy

This is a duplicate of: this question

这是重复的:这个问题

You can redirect stdout, stderr and stdin using std::freopen.

您可以使用 重定向标准输出、标准错误和标准输入std::freopen

From the above link:

从上面的链接:

/* freopen example: redirecting stdout */
#include <stdio.h>

int main ()
{
  freopen ("myfile.txt","w",stdout);
  printf ("This sentence is redirected to a file.");
  fclose (stdout);
  return 0;
}

You can also run your program via command prompt like so:

您还可以通过命令提示符运行您的程序,如下所示:

a.exe > stdout.txt 2> stderr.txt

回答by John T

If you want all output in a text file you don't need to code anything extra.

如果您希望所有输出都在一个文本文件中,则不需要编写任何额外的代码。

from the command line:

从命令行:

program > output.txt

If you only wish to redirect certain output you can use ostream as dirkgently suggested.

如果您只想重定向某些输出,您可以按照直接建议的方式使用 ostream。

回答by dirkgently

Then you cannot use std::cout anywhere else to print stuff from your program. Change std::cout to a std::ostream and then pass your file or std::cout as required.

然后你不能在其他任何地方使用 std::cout 从你的程序中打印内容。将 std::cout 更改为 std::ostream,然后根据需要传递您的文件或 std::cout。

回答by shoosh

sOutFile << stdout;

in C "stdout" is defined as a FILE*variable. It's just a pointer. Outputing it to a file just writes the value of the pointer, in your case: 0x77c5fca0to the file.

在 C 中“ stdout”被定义为一个FILE*变量。它只是一个指针。将其输出到文件只会将指针的值写入您的情况:0x77c5fca0写入文件。

If you want to direct your output to a file either write it the a file in the first place or redirect the output of your program to a file using the command line.

如果要将输出定向到文件,请首先将其写入文件,或者使用命令行将程序的输出重定向到文件。