文件流类:
(1) ifstream ifile:只输入用,它是从istream 类派生的。
(2) ofstream ofile:只输出用,它是从ostream 类派生的。
(3) fstream iofile:既输入又输出用,它是从iostream 类派生的。
//应用举例
#include"iostream.h"
#include "fstream.h"
#include "stdlib.h"
void main()
{char s[100];
ofstream outf;
outf.open("dialogue.dat");
if(!outf)
{cout<<"can't open dialogue.dat"<<endl;
abort();
}
outf<<"How are you?\n"; //通过插入操作将若干字符串写到文件中
outf<<"I'm fine,thank you.and you?\n";
outf<<"I'm very well.\n";
outf.close();
ifstream inf("dialogue.dat");
while(!inf.eof())
{inf.getline(s,sizeof(s)); //通过getline 函数从文件中读一行
//字符串到数组
cout<<s<<endl;
}
}