#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。