![]() |
#2
qq10235692232011-05-07 15:29
|

#include <fstream>
#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
int a[10],max,i,order;
ofstream outfile("exercise_4.txt",ios::out|ios::trunc);
if(!outfile)
{
cerr<<"can't open this file"<<endl<<"open error"<<endl;
getchar();
exit(0);
}
cout<<"put 10 number in this:"<<endl;
for(i=0;i<10;i++)
{cin>>a[i];
outfile<<a[i]<<" ";}
outfile.close();
//上半部分
ifstream infile("exercise_4.txt",ios::in);
if(!infile)
{
cerr<<"can't open this file"<<endl<<"open error"<<endl;
getchar();
exit(0);
}
for(i=0;i<10;i++)
{
infile>>a[i];
cout<<a[i]<<" "<<endl;
}
cout<<endl;
max=a[0];
for(i=1;i<10;i++)
if(a[i]>max)
{
max=a[i];
order=i;
}
cout<<"max="<<max<<endl;
cout<<"order="<<order<<endl;
getch(); //这里我写getch();就不会在执行完上半部分后闪屏而过,用getchar();为什么就会闪屏?
infile.close(); //这两个函数不是差不多吗,只不过显示和不显示。。
cout<<"The porgram is end."<<endl;
return 0;
}
#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
int a[10],max,i,order;
ofstream outfile("exercise_4.txt",ios::out|ios::trunc);
if(!outfile)
{
cerr<<"can't open this file"<<endl<<"open error"<<endl;
getchar();
exit(0);
}
cout<<"put 10 number in this:"<<endl;
for(i=0;i<10;i++)
{cin>>a[i];
outfile<<a[i]<<" ";}
outfile.close();
//上半部分
ifstream infile("exercise_4.txt",ios::in);
if(!infile)
{
cerr<<"can't open this file"<<endl<<"open error"<<endl;
getchar();
exit(0);
}
for(i=0;i<10;i++)
{
infile>>a[i];
cout<<a[i]<<" "<<endl;
}
cout<<endl;
max=a[0];
for(i=1;i<10;i++)
if(a[i]>max)
{
max=a[i];
order=i;
}
cout<<"max="<<max<<endl;
cout<<"order="<<order<<endl;
getch(); //这里我写getch();就不会在执行完上半部分后闪屏而过,用getchar();为什么就会闪屏?
infile.close(); //这两个函数不是差不多吗,只不过显示和不显示。。
cout<<"The porgram is end."<<endl;
return 0;
}