用于内存与文件之间的数据传输的三个类:
- ifstream类:读文件
- ofstream类:写文件
- fstream 类:读、写文件
常用的判断错误的方法:
good(); 如果文件打开成功
bad(); 打开文件时发生错误
eof(); 到达文件尾
要执行文件的输入输出,须做以下几件事:
1.在程序中包含头文件fstream
2.建立文件流。建立文件流的过程就是定义流类的对象,例如:
ifstream in;
ofstream out;
fstream both;
分别定义了输入流对象in;输出流对象out,输入输出流对象both。
3.使用open()函数打开文件,也就是使某一文件与上面的某一流相联系。
open(filename.c_str(),ios::out); //若文件名不存在,则自动创建
open()函数原型如下:
void open (char* filename, int mode, int access=0);
第一个参数用于传递文件名;
第二个参数mode值表示文件的使用方式;
第三个参数打开文件的共享方式
filebuf::openprot; //默认的兼容共享方式(与MS_DOS文件兼容)
filebuf::sh_none; //独占,不共享
filebuf::sh_read; //读共享
filebuf::sh_write; //写共享
ios::in = 0x01,//供读, (ifstream默认的打开方式)
ios::out = 0x02, //供写,(ofstream默认的打开方式)
文件不存在则创建
若文件已存在则清空原内容
ios::ate = 0x04, //文件打开时,指针在文件尾部。可改变指针的位置,
常和in、out联合使用:
1.以out方式打开文件时:
如果文件不存在,创建文件,
如果文件存在,则会删除原来的文件
2.以in 方式打开文件时
如果文件不存在,打开失败
ios::app = 0x08, //供写(以追加的方式打开),文件不存在则创建,若文件已存在则在原文件内容后写入新的内容,指针位置总在最后
ios::trunc = 0x10, //在读写前先将文件长度截断为0(默认)
ios::binary = 0x80 //二进制格式文件
4.对open结果进行检查,检查打开操作是否成功
fout.is_open(); //用来判断文件打开是否成功(建立文件信息区)
if(fout.fail()); //判断流的状态
if(! fout.good()); //判断流的状态
if(! fout); //测定对象fout是否 建立/存在(fstream 类对象不能使用这种方式)
5.进行读写。在建立(或打开)的文件上执行所要求的输入或输出操作;
6.关闭文件流对象。
fout.close();
例如:
写文件:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream ost("E:\\Documents\\test4.txt");
if(!ost)
{
return 0;
}
ost<<111<<endl;
ost.close();
}
先写后读:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream ost;
ost.open("E:\\Documents\\test2.txt");
ost<<212434<<endl;
ost<<123456<<endl;
ost.close();
ifstream ist;
ist.open("E:\\Documents\\test2.txt");
int a,b;
ist>>a>>b;
ist.close();
cout<<a<<endl<<b<<endl;
}
文件复制:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char ch;
ifstream ist("E:\\Documents\\copy.txt");
if(!ist) return 0;
ofstream ost("E:\\Documents\\copynew.txt");
if(!ost) return 0;
while(ist&&ist.get(ch))
{
ost.put(ch);
}
ist.close();
ost.close();
cout<<"ok!"<<endl;
}
建立学生的文本文件:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
string name;
int num,score;
ofstream ost("E:\\Documents\\test5.txt");
if(!ost) return 0;
ost<<"学生成绩文件:"<<endl;
while(cin>>num>>name>>score)
{
ost<<num<<" "<<name<<" "<<score<<endl;
}
ost.close();
}
学生信息操作:
#include <iostream>
#include<fstream>
#include <string>
using namespace std;
struct student{
string name ;
int number , score ;
};
class FileOperation
{
string filename;
student record[100]; //假设文件中存储的及记录个数不超过100
int num;//存储记录的个数
public:
FileOperation();
void display();
void append();
~FileOperation();
};
FileOperation::FileOperation(){
cin>>filename;
ifstream instuf(filename.c_str(),ios::in);
num=0;
if (!instuf)
{exit(0);}
instuf >>record[num].number>>record[num].name>>record[num].score;
while(!instuf.eof())
{
num++;
instuf>>record[num].number>>record[num].name>>record[num].score;
}
instuf.close();
}
void FileOperation::display()
{
cout<<"number"<<"\t"<<"name"<<"\t"<<"score"<<endl;
for(int i=0;i<num;i++)
cout<<record[i].number<<'\t'<<record[i].name<<'\t'<<record[i].score<<'\n';
}
void FileOperation::append()
{
cin>>record[num].number>>record[num].name>>record[num].score;
num++;
}
FileOperation::~FileOperation()
{
ofstream outstuf ;
outstuf.open(filename.c_str(),ios::out);
if(!outstuf)
abort();
for(int i=0;i<num;i++)
outstuf<<record[i].number<<' '<<record[i].name<<' '<<record[i].score<<'\n';
outstuf.close() ;
}
int main()
{
FileOperation f;
f.append();
f.display();
return 0;
}