第11章 流类库与输入输出
11-3 使用 I/O 流以文本方式建立一个文件 test1.txt,写入字符“已成功写入文件!”,用其他字处理程序(例如 Windows 的记事本程序 Notepad)打开,看看是否正确写入。
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream file1("test1.txt");
file1<<"已成功写入文件!"<<endl;
file1.close();
return 0;
}
11-4 使用 I/O 流以文本方式打开 11-3 题建立的文件 test1.txt,读出其内容并显示出来,看看是否正确。
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char a[100];
ifstream file2("test1.txt");
if(!file2)
{
cout<<"fail to open";
return 1;
}
file2.getline(a,100);
cout<<a;
file2.close();
return 0;
}
11-5 使用 I/O 流以文本方式打开 11-3 题建立的文件 test1.txt,在文件后面添加字符“已成功添加字符!”,然后读出整个文件的内容显示出来,看看是否正确。
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream file1("test1.txt",ios::app); //在文件尾添加数据
file1<<"已成功添加字符!"<<endl;
file1.close();
char ch;
ifstream file2("test1.txt"); //程序输出文件内容
while(file2.get(ch))
cout<<ch;
file2.close();
return 0;
}
11-6 定义一个 Dog 类,包含体重和年龄两个成员变量及相应的成员函数。声明一个实例 dog1,体重为 5,年龄为10,使用I/O 流把dog1 的状态写入磁盘文件。再声明另一个实例 dog2,通过读文件把 dog1 的状态赋给 dog2。分别使用文本方式和二进制方式操作文件,看看结果有何不同;再看看磁盘文件的ASCII码有何不同。
#include<iostream>
#include<fstream>
using namespace std;
class dog
{
public:
dog(int weight, long days):itsWeight(weight),itsNumberDaysAlive(days){}
~dog(){}
int GetWeight()const { return itsWeight; }
void SetWeight(int weight) { itsWeight = weight; }
long GetDaysAlive()const { return itsNumberDaysAlive; }
void SetDaysAlive(long days) { itsNumberDaysAlive = days; }
private:
int itsWeight;
long itsNumberDaysAlive;
};
int main() // returns 1 on error
{
char fileName[80];
cout << "Please enter the file name: ";
cin >> fileName;
//ofstream fout(fileName);
ofstream fout(fileName,ios::binary);
if (!fout)
{
cout << "Unable to open " << fileName << " for writing./n";
return 1;
}
dog Dog1(5,10);
fout.write((char*) &Dog1,sizeof Dog1); //将对象Dog1的数据写入文件fileName
fout.close();
//ifstream fin(fileName);
ifstream fin(fileName,ios::binary);
if (!fin)
{
cout << "Unable to open " << fileName << " for reading./n";
return 1;
}
dog Dog2(2,2);
cout << "Dog2 weight: " << Dog2.GetWeight() << endl;
cout << "Dog2 days: " << Dog2.GetDaysAlive() << endl;
fin.read((char*) &Dog2, sizeof Dog2); //将对象Dog1的数据从文件fileName读出,随即将数据赋给对象Dog2
cout << "Dog2 weight: " << Dog2.GetWeight() << endl;
cout << "Dog2 days: " << Dog2.GetDaysAlive() << endl;
fin.close();
return 0;
}
11-9 编写程序实现如下功能:打开指定的一个文本文件,在每一行前加行号后将其输出到另一个文本文件中。
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<iomanip> //函数setw() 输出格式控制
using namespace std;
int main(){
/*(1)打开输入文件*/
ifstream infile;
infile.open("abc.txt");//outfile.open("abc.txt",ios::in); //默认方式,两种都可以
if(!infile){
cout<<"输入文件abc.txt打开失败!\n";
return 1;
}
/*(2)打开输出文件*/
ofstream outfile;
outfile.open("abc_back.txt",ios::out);
if(!outfile){
cout<<"输出文件abc_back.txt打开失败!\n";
return 1;
}
/*(3)复制行,并在行头添加行号*/
int num_line=1; //行号计数器
char buffer[256]; //行拷贝缓冲区
while(!infile.eof()){
/*(3.1)读取1行,进行判断*/
if (!infile.getline(buffer, sizeof(buffer))){
//cout<<"已经到达文件结尾,文件读取失败,退出循环!";
break;
}
/*(3.2)写出构造的新行*/
while(infile.getline(buffer,256))
{
outfile.width(10); //设置域宽为10
outfile << num_line++ << ". ";
outfile << buffer <<endl; //将从abc.txt读入的内容写入abc_back.txt中
}
}
/*(4)文件关闭*/
infile.close();
outfile.close();
cout<<"操作成功,程序结束!\n";
return 0;
}
11-10 使用宽输入流从一个有中文字符的文本文件中读入所有字符,统计每个字符出现的次数,将统计结果用宽输出流输出到另一个文本文件中。
#include <iostream>
#include <fstream>
#include <string>
#include <locale.h>
#include <map>
using namespace std;
int main()
{
//locale loc(".936");
//wcout.imbue(loc);
puts(setlocale(LC_CTYPE, "")); //设置中文编码方式
wifstream in("习题11-10输入.txt");
wofstream out("习题11-10输出.txt");
//in.imbue(loc);
//out.imbue(loc);
wstring line; //用来存储一行内容
map<wchar_t,int>counter;
while(getline(in,line))
{
for(int i=0;i<line.length();++i)
{
counter[line[i]]++;
}
}
map<wchar_t,int>::iterator itor;
int i=1;
for(itor=counter.begin();itor!=counter.end();++itor,++i) //迭代器
{
out<<itor->first<<"\t"<<itor->second<<"\t";
if(i%4==0){
out<<endl;
}
}
in.close();
out.close();
return 0;
}
本专栏为本人大二C++课程的习题作业和一些学习经验的分享,供大家参考学习。如有侵权请立即与我联系,我将及时处理。
参考书籍为:C++语言程序设计 第五版 -清华大学出版社- 郑莉,董渊、C++语言程序设计 第五版 -清华大学出版社- 郑莉,董渊(学生用书)
编译环境:Visual Studio 2019、Dev-C++
欢迎关注我的微信公众号,分享一些有趣的知识:程序猿的茶水间