📄 demo_4_file_txt_1.cpp
字号:
//****************************************************
// 一个含10个元素的整型数组,由键盘输入10个整数给数组,
// 并将数组存放在磁盘文件中,而后读该文件中数据到内存,
// 显示这些数据,并找到最大值者.
//****************************************************
# include <fstream.h>
# include <stdlib.h>
int main()
{
int a[10];
// ofstream outfile("File.txt");
// ofstream outfile("File.txt",ios::out);
// ofstream outfile("G:\\File.txt"); //注意文件目录中的转义字符
ofstream outfile;
outfile.open("File.txt");
// outfile.open("File.txt",ios::out);
// outfile.open("G:\\File.txt");
if(!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
cout<<"enter 10 integer numbers:"<<endl;
for(int i=0;i<10;i++)
{
cin>>a[i];
outfile<<a[i]<<" ";
}
outfile.close();
int max;
// ifstream infile("File.txt",ios::in);
ifstream infile;
infile.open("File.txt",ios::in);
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
for(i=0;i<10;i++)
{
infile>>a[i];
cout<<a[i]<<" ";
}
infile.close();
cout<<endl;
max=a[0];
for(i=1;i<10;i++)
{
if(a[i]>max)
{
max=a[i];
}
}
cout<<"max="<<max<<endl;
return 0;
}
/*
enter 10 integer numbers:
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
max=9
Press any key to continue
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -