黑马:文件操作(143~146)

这篇博客详细介绍了C++中如何进行文件操作,包括文本文件和二进制文件的读写。通过示例代码展示了如何使用ofstream和ifstream进行文件的打开、写入和读取,以及如何处理二进制文件。重点讲解了文本文件的写入和读取,以及二进制文件的保存和读取Person对象。
摘要由CSDN通过智能技术生成

程序运行时产生的数据属于临时数据,一旦运行结束就会被释放

通过文件可以将数据持久化

对文件操作需要包含头文件<fstream>

文件类型分为两种:

1.文本文件:文件以文本的ASC码形式存储在计算机中i

2.二进制文件:文件以二进制形式存储在计算机中,用户一般不能读取

操作文件的三大类

1.ofsream:写操作

2.ifstream:读操作

3.fstream:读写操作

1.文本文件 写文件

 

 

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
//文本文件 写文件
void test01() {
	//创建流对象
	ofstream ofs;

	//指定打开方式
	ofs.open("test.txt", ios::out);

	//写内容
	ofs << "姓名: 徐吉磊" << endl;
	ofs << "性别: 男" << endl;
	ofs << "年龄: 20" << endl;

	//关闭文件
	ofs.close();


}

int main(int argc,char**argv) {
		
	test01();
	

	system("pause");
	return  0;
}

运行之后什么也没有,要在运行按钮那里右键,打开文件夹位置才可以找到。

2.文本文件 读文件

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>


//文本文件 读文件
void test01() {
	//创建流对象
	ifstream ifs;

	//指定打开方式,判断是否打开成功
	ifs.open("test.txt", ios::in);
	if (!ifs.is_open()) {
		cout << "打开文件失败" << endl;
		return;
	}

	//读数据

	//第一种
	/*char buf[1024] = { 0 };
	while (ifs >> buf) {
		cout << buf << endl;
	}*/

	//第二种
	/*char buf[1024] = { 0 };
	while (ifs.getline(buf, sizeof(buf))) {
		cout << buf << endl;
	}*/

	//第三种
	string buf;
	while ( getline(ifs, buf)) {
		cout << buf << endl;
	}


	//关闭文件
	ifs.close();


}

int main(int argc,char**argv) {
		
	test01();
	

	system("pause");
	return  0;
}

is_open是个函数
 

3.二进制文件 写文件

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>


class Person {
public:
	char m_Name[64];
	int m_Age;

};

void test01() {
	/*ofstream ofs;
	ofs.open("Person./txt", ios::out | ios::binary);*/

	ofstream ofs("Person.txt", ios::out | ios::binary); //这样写也可以 缩减为一行

	Person p = { "张三",18 };
	ofs.write((const char*)&p,sizeof(Person));

	ofs.close();


}


int main(int argc,char**argv) {
		
	test01();
	

	system("pause");
	return  0;
}

 

4.二进制文件 读文件

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>


class Person {
public:
	char m_Name[64];
	int m_Age;

};

void test01() {

	ifstream ifs;
	 
	ifs.open("Person.txt", ios::in | ios::binary);

	if (!ifs.is_open()) {
		cout<< "文件打开失败" << endl;
		return;
	}

	Person p;

	ifs.read((char*)&p, sizeof(Person));
	cout << "姓名: " << p.m_Name << "年龄: " << p.m_Age << endl;

	ifs.close();


}


int main(int argc,char**argv) {
		
	test01();
	

	system("pause");
	return  0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值