#include <cstdlib>
#include <iostream>#include <fstream>
//目的在於從number.txt文件中讀取數字,並比較大小 ,將結果輸入end.txt
using namespace std;
int main(int argc, char *argv[])
{ int score[5],max,i;
ifstream outf;
outf.open("number.txt");
outf>>score[0];
max=score[0];
for(i=1;i<5;i++)
{ outf>>score[i];
cout<<score[i]<<"/n";
if(max<score[i])
max=score[i];
}
cout<<max;
system("PAUSE");
return EXIT_SUCCESS;
}
上面是我剛寫的一個程序,目的可見註釋。我將此作爲一個實驗,得出一個結論
ofstream的讀取是有順序 ,如果之前讀取過這個數字,之後就不會讀取,當然前提是在一個模塊中。 .我的number.txt文件中數字是1 2 3 4 5;
最後屏幕顯示的是2/n3/n4/n5/n5,可見,在一開始max=score[0]的事件當中,已經用過了文件中的第一數,而後,不會再用到這個數。所以,在循環體中輸出的是score[1]-------- 2。