freopen()重定向的打开和关闭

freopen函数

功能

使用不同的文件或模式重新打开流,即重定向。

实现重定向,把预定义的标准流文件定向到由path指定的文件中。直观感觉/实际操作都像是把文件定向到流,难道是说,对流来说就是重定向,大雾)。

如果指定了新文件名,则该函数首先尝试关闭已与stream(第三个参数)关联的任何文件并取消关联。然后,无论该流是否成功关闭,freopen都会打开由filename指定的文件,并将其与关联,就像fopen使用指定的模式一样。(先记住后面有用)

参数

文件名

即要打开的文件的名字。

其值应遵循运行环境的文件名规范,并且可以包含路径(如果系统支持)。

模式

使用上面的模式说明符,文件将作为文本文件打开。为了打开一个文件作为二进制文件中,“b”的字符必须被包括在模式串。这个附加的“b”字符可以附加在字符串的末尾(从而产生以下复合模式:“rb”,“wb”,“ab”,“r + b”,“w + b”,“a + b“)或插入字母和混合模式的”+“符号之间(”rb +“,”wb +“,”ab +“)。

这里主要用标准流文件,标准流文件具体是指stdin、stdout和stderr。其中stdin是标准输入流,默认为键盘;stdout是标准输出流,默认为屏幕;stderr是标准错误流,一般把屏幕设为默认。通过调用freopen,就可以修改标准流文件的默认值,实现重定向。

实例

这个方法的好处十分明显,freopen之后,就能像平常一样使用scanf,printf,cin,cout

清单一:C++版

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     int a, b;
 7     freopen("in.txt", "r", stdin);
 8     freopen("out.txt", "w", stdout);
 9     while (cin >> a >> b)
10         cout << a + b << endl;
11     fclose(stdin);
12     fclose(stdout);
13 
14     return 0;
15 }

清单二:C版

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     int a, b;
 7     freopen("in.txt", "r", stdin);
 8     freopen("out.txt", "w", stdout);
 9     while (scanf("%d%d", &a, &b) == 2)
10         printf("%d\n", a + b);
11     fclose(stdin);
12     fclose(stdout);
13 
14     return 0;
15 }

清单三:带路径的输入输出文件

我用的VS2017,默认在工程文件夹下,只要路径写对,可以在任意文件夹下(本人测试只能放在该工程文件夹下的任意文件夹)

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     int a, b;
 7     freopen("in.txt", "r", stdin);
 8     freopen("Debug\\out.txt", "w", stdout);
 9     while (scanf("%d%d", &a, &b) == 2)
10         printf("%d\n", a + b);
11     fclose(stdin);
12     fclose(stdout);
13 
14     return 0;
15 }

清单四:竞赛常用版

比如,杭电1000题我完全可以这样提交:

#include <stdio.h> 
#include <iostream> 

using namespace std;
int main() 
{ 
#ifdef ONLINE_JUDGE
#else
    freopen("in.txt","r",stdin);
#endif
    int a,b;
    while(cin>>a>>b)
        cout<<a+b<<endl;
    return 0;
}

在本地机器调试时,因为没有定义过ONLINE_JUDGE,所以会执行freopen("in.txt","r",stdin);方便本机上的调试,当提交到OJ上后,因为有了ONLINE_JUDGE的定义,所以跳过语句freopen("in.txt","r",stdin); 从 int a,b;处开始执行。

freopen的“关闭”

在写代码时常出现这种情况:我们从原有文件使用freopen导入数据,但之后关闭文件再次从键盘输入。我们如果直接fclose(stdin),之后的键盘输入肯定不管用。应如何解决?

显然,如果在使用完freopen之后,如果还需要使用标准输入输出,不能把它们直接fclose。

我们不妨再次重定向,把stdin、stdout重定向到控制台,就能从键盘接受输入、从屏幕输出。

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     int a, b;
 7     freopen("in.txt", "r", stdin);
 8     freopen("Debug\\out.txt", "w", stdout);
 9     while (scanf("%d%d", &a, &b) == 2)
10         printf("%d\n", a + b);
11     //fclose(stdin);
12     //fclose(stdout);
13     freopen("CON", "r", stdin);
14     freopen("CON", "w", stdout);
15     printf("Hello World\n");
16     scanf("%d%d", &a,&b);
17 
18     return 0;
19 }

需要注意,这里其实没有真正关闭,只是再次重定向,回到控制台。

在windows/DOS,读文件后用freopen("CON", "r", stdin),写文件后  freopen("CON", "w", stdout)。

在linux中,控制台设备是 /dev/console:freopen("/dev/console", "r", stdin)。

参考链接:

1、https://blog.csdn.net/SJF0115/article/details/7695723

2、http://www.cplusplus.com/reference/cstdio/freopen/

3、https://blog.csdn.net/xylon_/article/details/81257268

4、https://zhidao.baidu.com/question/475250525.html

5、http://www.voidcn.com/article/p-ymjofuqn-rs.html

猜你喜欢

转载自www.cnblogs.com/lfri/p/10163062.html