C++读取文本文件

Description

在文件f1.dat中,提供了N(N是一个很大的数,程序中不需要用到)个正整数。请编程序,输出文件中前n(n<N)个数中的最大值。
f1.dat中的前10个数据如下,请在调试程序时,自建f1.dat文件,其内容是10个整数。
52
69
21
29
65
79
72
27
35
24


 

Input

整数n,代表输出的最大值是f1.dat文件中前n个数中的最大值

 

Output

f1.dat文件中前n个数中的最大值。由于f1.dat已经在题目中给定,这个最大值取决于文件内容。就题目描述部分给出的数据:输入n为5时,输出“max number: 69”;输入n为8时,输出“max number: 79”。

 

Sample Input

5

Sample Output

max number: 69




 1 #include<iostream>
 2 #include<fstream>
 3 using namespace std;
 4 int main()
 5 {
 6     int a[10]={52,69,21,29,65,79,72,27,35,24},max,j,n;
 7     fstream f1;
 8     ofstream outfile("f1.dat",ios::out);
 9         for(int i=0;i<10;i++)
10         outfile<<a[i]<<" ";
11         outfile.close();
12         ifstream infile("f1.dat");
13             for(j=0;j<10;j++)
14             {infile>>a[j];}
15             cin>>n;
16             max=a[0];    
17             for(j=1;j<(n-1);j++)
18             {    if(a[j]>max)
19             max=a[j];}
20                 cout<<"max number: "<<max<<endl;
21                 infile.close();
22                 return 0;
23 }

 

posted @ 2014-06-12 12:49  浅水戏虾sty  阅读(451)  评论(0编辑  收藏  举报