输入输出流
zhu:cheng se bu fen shi cheng xu de guan jian bu fen !!!
对ASCII文件的操作
1 写数据进入磁盘文件
eg: 从键盘中输入10个数存入数组,输入进磁盘文件
#include <fstream>//因为要进行对文件的写操作
#include <iostream>
using namespace std;
int main()
{
ofstream out;//建立输出流对象
out.open("l.text",ios::out) ;//以写的方式打开文件
if(!out)//判断文件是否打开成功
{
cerr<<"打开文件失败" <<endl;
exit(1);//停止程序
}
int a[10] ;
for(int i=0;i<10;i++)
{
cin>>a[i];//键盘输入给数组
}
for(int i=0;i<10;i++)
{
out<<a[i]<<" ";//使用定义的流对象,将数据从键盘输出到磁盘文件从存放
}
out.close() ;//文件关闭;
return 0;
}
(2)从文件中读取数据
eg:从上个题中的文件读取10个数的数据并找出最大值和它所在的数组的序号
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream infile;//建立输入流对象
infi