#include <stdio.h> // 实际使用中发现freopen也包含在iostream中,C++代码#include <iostream>即可。
int main()
{
freopen("sample.in", "r", stdin);
freopen("sample.out", "w", stdout);
/* 同控制台输入输出 */
fclose(stdin);
fclose(stdout);
return 0;
}
函数名:freopen
声明:FILE *freopen( const char *path, const char *mode, FILE *stream );
所在文件: stdio.h
参数说明:
path: 文件名,用于存储输入输出的自定义文件名。
mode: 文件打开的模式。和fopen中的模式(如r-只读, w-写)相同。
stream: 一个文件,通常使用标准流文件。
返回值:成功,则返回一个path所指定文件的指针;失败,返回NULL。(一般可以不使用它的返回值)
功能:实现重定向,把预定义的标准流文件定向到由path指定的文件中。标准流文件具体是指stdin、stdout和stderr。其中stdin是标准输入流,默认为键盘&