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)。
強調(freopen亂碼的原因):
如果你寫的程序中輸出了中文,則重定向會出現亂碼,目前沒有解決方法,你可以使用英文代替中文(不過很麻煩)。
Tip:我本人試了許多方法,均無法解決亂碼問題。
參考鏈接:
1、https://www.cnblogs.com/lfri/
2、https://blog.csdn.net/SJF0115/article/details/7695723
3、http://www.cplusplus.com/reference/cstdio/freopen/
4、https://blog.csdn.net/xylon_/article/details/81257268
5、https://zhidao.baidu.com/question/475250525.html
6、http://www.voidcn.com/article/p-ymjofuqn-rs.html
Update:
第1次:2020年3月13日
第2次:2021年1月3日