在我的应用程序中,我想将通常转到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)
为了更快地输入,我读到你可以做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)
我错过了什么吗?