In some cases, programmer wants to redirect input or output as a file for debugging facility. This post will introduce how to redirect input and output in C and C++ program.
C Language
In C language, usually, scanf and printf are used for programming input and output in terminal respectively. To redirect them to a file, freopen() is a method.
|
|
The code redirects standard input to data.in and output to data.out.
C++ Language
In C++, iostream is commonly used for input and output, like std::cin and std::cout. For redirction, rdbuf() can assign them to a file.
|
|
The code above assign iostream to data.in and data.out.Then, it restores them to original streambuf.