1、编程实现,要求定义here 为buffer的对象,与输入文件file1.txt建立关联,文件file1.txt文件的内容如下:
we are from china.
定义there为buffer的对象,与输出文件file2.txt建立关联。当文件打开成功后将file1.txt文件的内容附加到file2.txt文件的尾部。运行前file2.txt文件的内容如下:
I'm a boy.
you're a girl.
运行后,再查看文件file2.txt的内容.
#include<iostream>
#include<fstream>
using namespace std;
class buffer
{
public:
void teat1()
{
ofstream ofs; //声明输入文件的ofs
ofs.open("file1.txt", ios::out ); //打开文件,只写
ofs << "we are from china";
ofs.close(); //关闭文件
ofs.open("file2.txt", ios::out); //打开文件
ofs << "i am a boy " << endl;
ofs << "you are a girl" << endl;
ofs.close(); //关闭文件
}
void teat3()
{
ofstream ofs; //声明输入文件的ofs
ofs.open("file2.txt", ios::out | ios::app); //ios::app打开文件并续写
ofs << "we are from china";
ofs.close();
}
void teat2()
{
ifstream ifs; //声明输出文件的ifs
ifs.open("file1.txt", ios::in); //打开文件,只读
char buf[1024] = { 0 }; //把文件输入屏幕
while (ifs >> buf)
{
cout << buf<<" ";
}
cout << endl;
ifs.close();
ifs.open("file2.txt", ios::in);
char bu[1024] = { 0 };
while (ifs >> bu)
{
cout << bu <<" ";
}
cout << endl;
ifs.close();
}
};
int main(void)
{
buffer here; //声明对象here
here.teat1();
here.teat2();
buffer there; //声明对象there
there.teat1();
there.teat3();
there.teat2();
system("puase");
return 0;
}