将所有程序输出写入C ++中的txt文件 [英] Writing ALL program output to a txt file in C++

查看:615
本文介绍了将所有程序输出写入C ++中的txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

sOutFile << stdout;

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

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

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

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

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.

0x77c5fca0

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

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.

如果这个代码是一个地址,它会包含我的所有输出吗?

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?

推荐答案

这是重复的:此问题

您可以重定向stdout, stderr和stdin,使用 std :: freopen

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

从上面的链接:

/* 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

这篇关于将所有程序输出写入C ++中的txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆