第二十一章流 4文件的输入和输出

本文介绍了使用C++中的fstream库进行文件的输入输出操作,包括如何将数据输出到文件中,以及如何从文件中读取数据。通过实例演示了不同场景下的文件操作方法。
// 第二十一章流 4文件的输入和输出
// 1输出数据到文件
   //(1)包含fstream头文件
   //(2)建立ofstream对像  如: ofstream ocout
   //(3)将对像与文件关联 ocout.open('123.txt');
   //(4)该对像可看作cout对像,因此我们使用该对像将数据输出到文件中,而不是屏幕中 ocout<<"abc";
   //(5)关闭与文件的连接 count.close();
/*#include <iostream>
#include <fstream>
using namespace std;
int main()
{
	ofstream ofstr;
	ofstr.open("123.txt");
	ofstr<<"hello word";
	ofstr.close();
    return 0;
}*/

//2 读取文件中的数据
/*#include <iostream>
#include <fstream>
#include <ostream>
using namespace std;
int main()
{
	char temp[25] = {0};
	ifstream fin("123.txt");
	ofstream fout("124.txt");
	fin>>temp; //读取
	fout<<temp<<endl; //写入
	fout<<temp<<endl;
	fin.close();
	fout.close();

	cout<<"文件内容为:"<<temp<<endl;

    return 0;
}*/

// 4读取空格及空格后面的字符
/*#include <iostream>
#include <fstream>

using namespace std;
int main()
{
	const int num = 255;
	char temp1[num]={0};
	char temp2[num]={0};
	ofstream fout("1.txt");
	cout<<"请输入:"<<endl;
	cin.getline(temp1,num-1,0);
	temp1[num-1]='\0';
	fout<<temp1;
	fout.close();

	ifstream fin("1.txt"); //读取
	fin.getline(temp2,num-1,0); //读取内容
	fout.open("2.txt"); //打开2  
	fout<<temp2;        //写入到2中
	cout<<temp2<<endl;  //输出
	fout.close();       //关闭
    return 0;
}*/

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值