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
", 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
", 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
", 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 }
需要注意,這里其實沒有真正關閉,只是再次重定向,回到控制臺。
在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
總結
以上是生活随笔為你收集整理的freopen()重定向的打开和关闭的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: redhat 查看CPU frequen
- 下一篇: PV操作