聲明:這些東西基本上都不是原創的,我也沒辦法原創,只是一些整合,如果非得說原創,那恐怕只有C/C++標準是原創的。
freopen:
下面知識來自百度百科:
reopen是被包含於C標準庫頭文件<stdio.h>中的一個函數,用於重定向輸入輸出流。該函數可以在不改變代碼原貌的情況下改變輸入輸出環境,但使用時應當保證流是可靠的。
函數名:freopen
函數,以指定模式重新指定到另一個文件。模式用於指定新文件的訪問方式。
頭文件:stdio.h
C89函數聲明:
FILE *freopen( const char *filename, const char *mode, FILE *stream );
C99函數聲明:
形參說明:
filename:需要重定向到的文件名或文件路徑。
mode:代表文件訪問權限的字符串。例如,"r"表示“只讀訪問”、"w"表示“只寫訪問”、"a"表示“追加寫入”。
stream:需要被重定向的文件流。
返回值:如果成功,則返回該指向該輸出流的文件指針,否則返回爲NULL。
應用舉例:
從文件in.txt中讀入數據,計算加和輸出到out.txt中。
#include <stdio.h>
int main(void)
{
int a, b;
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
while (scanf("%d%d", &a, &b) != EOF)
printf("%d\n",a+b);
fclose(stdin);
fclose(stdout);
return 0;
}
{
int a, b;
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
while (scanf("%d%d", &a, &b) != EOF)
printf("%d\n",a+b);
fclose(stdin);
fclose(stdout);
return 0;
}
然後我們運行一下,發現黑框框裏什麼都沒有,好吧,這個函數的功能本來就是重定向輸入輸出流。
__________________________________這是未完的分割線_______________________________________________