相关疑难解决方法(0)

C++:重定向STDOUT

在我的应用程序中,我想将通常转到stdout流的输出重定向到我定义的函数.我读到你可以将stdio重定向到一个文件,为什么不用一个函数呢?

例如:

void MyHandler( const char* data );

//<<Magical redirection code>>

printf( "test" );
std::cout << "test" << std::endl;

//MyHandler should have been called with "test" twice, at this point
Run Code Online (Sandbox Code Playgroud)
  • 我怎样才能实现这种/类似的行为?

c++

48
推荐指数
5
解决办法
5万
查看次数

C++文件重定向

为了更快地输入,我读到你可以做file-redirection并包含cin已设置输入的文件.

理论上它应该像下面这样使用

App.exe inputfile outputfile
Run Code Online (Sandbox Code Playgroud)

据我在C++ Primer一书中所理解,以下C++代码[1]应该cin从文本文件中读取输入,不需要像[2]这样的任何其他特殊指示

[2]

include <fstream>
ofstream myfile;
myfile.open ();
Run Code Online (Sandbox Code Playgroud)

[1]以下C++代码......

#include <iostream>
int main(){
    int val;
    std::cin >> val; //this value should be read automatically for inputfile
    std::cout << val;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

c++ file

14
推荐指数
2
解决办法
2万
查看次数

标签 统计

c++ ×2

file ×1