如何通过程序更改标准输出

时间:2022-09-03 00:05:24

Program:

程序:

#include<stdio.h>
void main()
{
    printf("Hello world\n");
}

The above program print the output as "Hello world" in stdout(Terminal). But, I need the output of the program in some other file like "output.txt". So, is there any way to change the standard output of the process to some other file via programatically.

上面的程序在stdout(终端)中将输出打印为“Hello world”。但是,我需要在其他文件中输出程序,如“output.txt”。那么,有没有办法通过编程方式将进程的标准输出更改为其他文件。

3 个解决方案

#1


1  

You don't need all this stdout changing business. Everything you have to do is to open a file and then write to it. Use fopen and fprintf to do it.

你不需要所有这些改变业务的stdout。您需要做的就是打开一个文件,然后写入文件。使用fopen和fprintf来完成它。

#2


1  

You might want to use freopen(3) on stdout but it would close stdout.

你可能想在stdout上使用freopen(3),但它会关闭stdout。

You could use dup2(2) like:

您可以使用dup2(2),如:

int newfd = open("/tmp/someoutput.txt", O_WRONLY|O_CREAT, 0640);
if (newfd<0) {
  perror("/tmp/someoutput.txt"); exit(EXIT_FAILURE); };
if (dup2(STDOUT_FILENO, newfd)) {
  perror("dup2"); exit(EXIT_FAILURE); }

But as commented by David Heffernan you really want to use redirections in your shell.

但正如David Heffernan评论的那样,你真的想在你的shell中使用重定向。

IMHO redirecting STDOUT_FILENO like above smells bad.

恕我直言重定向STDOUT_FILENO如上所述闻起来很糟糕。

A possible way might be to declare a global

可能的方法是声明全局

FILE* myout = stdout;

and use always fprintf(myout, instead of printf( and perhaps sometimes doing myout = fopen("/tmp/someoutput.txt"); with a test!

并始终使用fprintf(myout,而不是printf(也许有时候做myout = fopen(“/ tmp / someoutput.txt”);测试!

At least inform the user that you are redirecting his stdout (perhaps by some message to stderr etc...) !

至少告知用户你正在重定向他的stdout(可能是通过某些消息给stderr等等)!

#3


0  

You can make use of fprintf to write in a file .Open file in w or a mode and then use fprintf to write in it .

您可以使用fprintf写入文件。以w或模式打开文件,然后使用fprintf写入其中。

#1


1  

You don't need all this stdout changing business. Everything you have to do is to open a file and then write to it. Use fopen and fprintf to do it.

你不需要所有这些改变业务的stdout。您需要做的就是打开一个文件,然后写入文件。使用fopen和fprintf来完成它。

#2


1  

You might want to use freopen(3) on stdout but it would close stdout.

你可能想在stdout上使用freopen(3),但它会关闭stdout。

You could use dup2(2) like:

您可以使用dup2(2),如:

int newfd = open("/tmp/someoutput.txt", O_WRONLY|O_CREAT, 0640);
if (newfd<0) {
  perror("/tmp/someoutput.txt"); exit(EXIT_FAILURE); };
if (dup2(STDOUT_FILENO, newfd)) {
  perror("dup2"); exit(EXIT_FAILURE); }

But as commented by David Heffernan you really want to use redirections in your shell.

但正如David Heffernan评论的那样,你真的想在你的shell中使用重定向。

IMHO redirecting STDOUT_FILENO like above smells bad.

恕我直言重定向STDOUT_FILENO如上所述闻起来很糟糕。

A possible way might be to declare a global

可能的方法是声明全局

FILE* myout = stdout;

and use always fprintf(myout, instead of printf( and perhaps sometimes doing myout = fopen("/tmp/someoutput.txt"); with a test!

并始终使用fprintf(myout,而不是printf(也许有时候做myout = fopen(“/ tmp / someoutput.txt”);测试!

At least inform the user that you are redirecting his stdout (perhaps by some message to stderr etc...) !

至少告知用户你正在重定向他的stdout(可能是通过某些消息给stderr等等)!

#3


0  

You can make use of fprintf to write in a file .Open file in w or a mode and then use fprintf to write in it .

您可以使用fprintf写入文件。以w或模式打开文件,然后使用fprintf写入其中。