用c++将所有程序输出写到txt文件中

时间:2022-12-04 17:32:08

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.

当我将stdout插入到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?

我怎样才能将我的程序输出写入文本文件?

5 个解决方案

#1


7  

This is a duplicate of: this question

这是这个问题的重复

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

您可以使用std来重定向stdout、stderr和stdin::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

#2


15  

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

#3


6  

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.

如果您只希望重定向某些输出,您可以按照dirkgentle的建议使用ostream。

#4


3  

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。

#5


3  

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: 0x77c5fca0 to the file.

在C中,“stdout”被定义为一个文件*变量。它只是一个指针。将它输出到一个文件中,只需要写入指针的值,在您的例子中: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.

如果您想要将输出指向一个文件,您可以首先将其写入文件,或者使用命令行将程序的输出重定向到一个文件。

#1


7  

This is a duplicate of: this question

这是这个问题的重复

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

您可以使用std来重定向stdout、stderr和stdin::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

#2


15  

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

#3


6  

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.

如果您只希望重定向某些输出,您可以按照dirkgentle的建议使用ostream。

#4


3  

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。

#5


3  

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: 0x77c5fca0 to the file.

在C中,“stdout”被定义为一个文件*变量。它只是一个指针。将它输出到一个文件中,只需要写入指针的值,在您的例子中: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.

如果您想要将输出指向一个文件,您可以首先将其写入文件,或者使用命令行将程序的输出重定向到一个文件。