C++ :类和对象:文件操作

前言:

程序运行时产生的数据都属于临时数据,程序一旦运行结束,数据都会被释放。通过文件可以 将数据持久化,C++ 中对文件操作需要包含头文件 <fstream>。

文件类型分为两种:

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

2:二进制文件:文件以文本的二进制形式存在在计算机中,用户一般不能直接读懂他们

头文件<fstream>定义了三个类型来操作文件。

1:ofstream:写操作

2:ifstream:读操作

3:fsstream :读写操作

1:文本文件 

1.1:写 文件

写文件步骤

1:包含头文件: <fstream>

2:  创建流对象 ofstream ofs

3:  打开文件  ofs.open("文件路径", 打开方式)

4:写数据 ofs<< "写入数据";

5: 关闭文件 ofs.close()

打开方式解释
ios::in为读文件而打开文件
ios::out为写文件而打开文件
ios::ate初始位置:文件尾
ios::app追加方式写文件
ios::trunc如果文件存在先删除,再创建
ios::binary二进制方式
#include<fstream>
using namespace std;

void test() {
	ofstream ofs;
	// 打开文件,并写入内容
	ofs.open("text.txt", ios::out);
	ofs << "姓名:张三" << endl;
	ofs << "性别:男" << endl;
	ofs << "年龄:18" << endl;
	ofs.close();
}
int main() {
	test();
}

 

1.2 读文件 

读文件与写文件步骤相似,但是读取方式相对于比较多,读文件步骤如下

1:包含头文件:#include<fstream>

2:  创建流对象: ifstream ifs

3:  打开文件并判断文件是否打开成功   ifs.open("文件路径",打开方式)

4:读数据:四种读取方式

5:关闭流 ifs.close()

#include<fstream>
#include<istream>
#include<iostream>
#include<string>
using namespace std;

void test() {
	ofstream ofs;
	// 打开文件,并写入内容
	ofs.open("text.txt", ios::out);
	ofs << "姓名:张三" << endl;
	ofs << "性别:男" << endl;
	ofs << "年龄:18" << endl;
	ofs.close();
}

void test2(){
	// 创建文件流对象,并打开文件
	ifstream ifs;
	ifs.open("text.txt", ios::in);
	if (!ifs.is_open())
	{
		 cout<< "打开文件失败" << endl;
		return;
	}

	// 方式一: 通过 while循环一行一行的读取,利用右移运算符将数据放到buf 中
	//char buf[1024] = { 0 };
	//while (ifs >> buf)
	//{
	//	cout << buf << endl;
	//}
	//ifs.close();

	// 方式二: 通过getline指定要存放数据的buf和要读取的字节
	char buf2[1024] = { 0 };
	cout << "要读取的字节:" << sizeof(buf2)<< endl;  // 打印 1024
	int count = 0;
	// 从istream中读取至多n个字符(包含结束标记符)保存在s对应的数组中。即使还没读够n个字符,如果已经没有字符可读,则读取终止
	while (ifs.getline(buf2,sizeof(buf2)))
	{
		count++;
		cout << buf2 << endl;
	}
	cout << "读取的次数:" << count << endl;  // 打印 3
	ifs.close();

	//第三种方式
	//把所有数据放到string中,通过一个全局函数getline来读取
	//全局函数getline的第一个参数为文件流对象,第二个参数为存放数据的buf
	/*string buf3;
	while (getline(ifs, buf3))
	{
		cout << buf3 << endl;
	}*/

}
int main() {
	test2();
	return 0;
}

2: 二进制文件 

以二进制文件的方式对文件进行读写操作,打开文件的方式要指定为: ios::binary。

2.1 :写文件

二进制方式写文件主要利用流对象调用成员函数 write

函数原型:  ostream& write(const char* buffer,  int length);

参数解释:字符指针buffer 指向内存中一段存储空间,length是读写的字节数。

#include<istream>
#include<fstream>
#include<string>
using namespace std;

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

void test() {
	ofstream ofs("person.txt", ios::out | ios::binary);
	Person p = { "张三",18 };
	ofs.write((const char*)&p, sizeof(p));
	ofs.close();
}

int main() {
	test();
}

 2.2 读文件

二进制放回读文件主要利用流对象调用成员函数 read

函数原型:istream& read(char* buffer, int length);

参数解释:字符指针buffer 指向内存中一段存储空间,length是读写的字节数

#include<istream>
#include<fstream>
#include<string>
#include<iostream>
using namespace std;

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

void test() {
	ifstream ifs("person.txt", ios::out | ios::binary);
	if (!ifs.is_open())
	{
		cout << "文件打开失败" << endl;
	}
	Person p;
	ifs.read((char*)&p, sizeof(p));
	ifs.close();
	cout << "姓名:" << p.m_Name << " 年龄:" << endl;
}

int main() {
	test();
}

 

 打印结果可知:可以正常读出 person.txt 文件里面内容。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值