gpt4 book ai didi

c++ - 将标准输出重定向回控制台

转载 作者:太空狗 更新时间:2023-10-29 23:11:00 27 4
gpt4 key购买 nike

关于将 stdout 和 stderr 重定向到文件而不是控制台的文档很多。你如何将它重新重定向回来?下面的代码显示了我的意图,但只输出一次“stdout is printed to console”。

我猜我需要获取控制台输出缓冲区,将其存储在某处,将标准输出重定向到文件,然后恢复控制台缓冲区?

#pragma warning(disable:4996)

#include <cstdio>

int main()
{
std::printf("stdout is printed to console\n");

if (std::freopen("redir.txt", "w", stdout)) {
std::printf("stdout is redirected to a file\n"); // this is written to redir.txt
std::fclose(stdout);

std::printf("stdout is printed to console\n");
}

getchar();
return 0;
}

最佳答案

感谢上面评论中的文章,我找到了我需要的信息。 dup 和 dup2 函数正是我所需要的。请注意,基于信息 here dup 和 dup2 已弃用,取而代之的是 _dup 和 _dup2。可以在 MSDN here 上找到一个工作示例, 但在下面复制以防将来链接断开。

// crt_dup.c
// This program uses the variable old to save
// the original stdout. It then opens a new file named
// DataFile and forces stdout to refer to it. Finally, it
// restores stdout to its original state.

#include <io.h>
#include <stdlib.h>
#include <stdio.h>

int main( void )
{
int old;
FILE *DataFile;

old = _dup( 1 ); // "old" now refers to "stdout"
// Note: file descriptor 1 == "stdout"
if( old == -1 )
{
perror( "_dup( 1 ) failure" );
exit( 1 );
}
_write( old, "This goes to stdout first\n", 26 );
if( fopen_s( &DataFile, "data", "w" ) != 0 )
{
puts( "Can't open file 'data'\n" );
exit( 1 );
}

// stdout now refers to file "data"
if( -1 == _dup2( _fileno( DataFile ), 1 ) )
{
perror( "Can't _dup2 stdout" );
exit( 1 );
}
puts( "This goes to file 'data'\n" );

// Flush stdout stream buffer so it goes to correct file
fflush( stdout );
fclose( DataFile );

// Restore original stdout
_dup2( old, 1 );
puts( "This goes to stdout\n" );
puts( "The file 'data' contains:" );
_flushall();
system( "type data" );
}

关于c++ - 将标准输出重定向回控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52737554/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com