【中南林业科技大学】《陈》大一下第九周实验作业 文件输出输入流。

5 篇文章 0 订阅
1 篇文章 0 订阅

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;
}

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值