//例7.12 将'a'至'z' 的26个英文字母写入文本文件,而后从该文件中读出并显示出来。
#include
#include
#include
using namespace std;
int main()
{ (1) outf("f3.txt", ios::out );
//定义输出文件流对象outf,输出方式打开文本文件f3.txt
if (!outf) //如果文件打开失败,outf返回0值
{ cout<<"Cannot open output file\n,"; exit(1); }
char ch='a';
for(int i=0;i<26;i++)//将'a'至'z'的26个英文字母写入文件
{ (2)
ch++;
}
(3) //关闭文件
ifstream (4) ( (5) , (6) );
//定义输入文件流对象inf,打开输入文本文件f3.txt
if (!inf ) //如果文件打开失败
{ cout<<"Cannot open input file\n,"; exit(1);}
while( (7) ) //流类的函数eof()返回值为零表示文件没结束
{ (8) //从文件中读出一个字符
cout<<ch; }
//while( inf.get(ch) )//从文件中读出字符,读取成功则get函数返回非0值,如失败(遇文件结束符EOF)则函数返回0值
// cout<<ch;
inf.close(); //关闭文件
return 0;
}
题目中就有答案
1 ofstream
2 outf << ch;
3 outf.close();
4 inf
5 "f3.txt"
6 ios::in
7 !inf.eof()
8 inf.get(ch)