freopen()重定向的打开和关闭
freopen函數(shù)
功能
使用不同的文件或模式重新打開流,即重定向。
實現(xiàn)重定向,把預(yù)定義的標(biāo)準(zhǔn)流文件定向到由path指定的文件中。(直觀感覺/實際操作都像是把文件定向到流,難道是說,對流來說就是重定向,大霧)。
如果指定了新文件名,則該函數(shù)首先嘗試關(guān)閉已與stream(第三個參數(shù))關(guān)聯(lián)的任何文件并取消關(guān)聯(lián)。然后,無論該流是否成功關(guān)閉,freopen都會打開由filename指定的文件,并將其與流關(guān)聯(lián),就像fopen使用指定的模式一樣。(先記住后面有用)
參數(shù)
文件名
即要打開的文件的名字。
其值應(yīng)遵循運行環(huán)境的文件名規(guī)范,并且可以包含路徑(如果系統(tǒng)支持)。
模式
使用上面的模式說明符,文件將作為文本文件打開。為了打開一個文件作為二進制文件中,“b”的字符必須被包括在模式串。這個附加的“b”字符可以附加在字符串的末尾(從而產(chǎn)生以下復(fù)合模式:“rb”,“wb”,“ab”,“r + b”,“w + b”,“a + b“)或插入字母和混合模式的”+“符號之間(”rb +“,”wb +“,”ab +“)。
流
這里主要用標(biāo)準(zhǔn)流文件,標(biāo)準(zhǔn)流文件具體是指stdin、stdout和stderr。其中stdin是標(biāo)準(zhǔn)輸入流,默認(rèn)為鍵盤;stdout是標(biāo)準(zhǔn)輸出流,默認(rèn)為屏幕;stderr是標(biāo)準(zhǔn)錯誤流,一般把屏幕設(shè)為默認(rèn)。通過調(diào)用freopen,就可以修改標(biāo)準(zhǔn)流文件的默認(rèn)值,實現(xiàn)重定向。
實例
這個方法的好處十分明顯,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
", a + b);
11 fclose(stdin);
12 fclose(stdout);
13
14 return 0;
15 }
清單三:帶路徑的輸入輸出文件
我用的VS2017,默認(rèn)在工程文件夾下,只要路徑寫對,可以在任意文件夾下(本人測試只能放在該工程文件夾下的任意文件夾)
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
", 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;
}
在本地機器調(diào)試時,因為沒有定義過ONLINE_JUDGE,所以會執(zhí)行freopen("in.txt","r",stdin);方便本機上的調(diào)試,當(dāng)提交到OJ上后,因為有了ONLINE_JUDGE的定義,所以跳過語句freopen("in.txt","r",stdin);從int a,b;處開始執(zhí)行。
freopen的“關(guān)閉”
在寫代碼時常出現(xiàn)這種情況:我們從原有文件使用freopen導(dǎo)入數(shù)據(jù),但之后關(guān)閉文件再次從鍵盤輸入。我們?nèi)绻苯觙close(stdin),之后的鍵盤輸入肯定不管用。應(yīng)如何解決?
顯然,如果在使用完freopen之后,如果還需要使用標(biāo)準(zhǔn)輸入輸出,不能把它們直接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
", a + b);
11 //fclose(stdin);
12 //fclose(stdout);
13 freopen("CON", "r", stdin);
14 freopen("CON", "w", stdout);
15 printf("Hello World
");
16 scanf("%d%d", &a,&b);
17
18 return 0;
19 }
需要注意,這里其實沒有真正關(guān)閉,只是再次重定向,回到控制臺。
在windows/DOS,讀文件后用freopen("CON", "r", stdin),寫文件后 freopen("CON", "w", stdout)。
在linux中,控制臺設(shè)備是 /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
總結(jié)
以上是生活随笔為你收集整理的freopen()重定向的打开和关闭的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: redhat 查看CPU frequen
- 下一篇: PV操作