5文件操作

本文详细介绍了C++中fstream库的三种操作方式:ofstream用于写文件,ifstream用于读文件,以及fstream进行读写操作。包括文本文件和二进制文件的处理,展示了如何使用头文件<fstream>进行文件操作,以及涉及自定义类型如dog的二进制文件读写示例。
摘要由CSDN通过智能技术生成

包含头文件<fstream>

操作文件三大类:

  1. ofstream : 写文件
  2. ifstream :读文件
  3. fstream : 读写文件

5.1文本文件

-文件以ascii的形式存储在计算机中

5.1.1写文件

步骤:

  1. 包含头文件    #include "fstream"
  2. 创建流对象    ofstream ofs;
  3. 打开文件       ofs.open(“文件格式”,打开方式);
  4. 写数据           ofs << "写入数据";
  5. 关闭文件       ofs.close();
文件打开方式
打开方式解释
ios::binary二进制方式
ios::in为读文件而打开文件
ios::out为写文件而打开文件
ios::ate初始位置:文件尾
ios::app追加方式写文件
ios::trunc如果文件存在先删除再创建

注意:文件打开方式可配合使用,利用|操作符

例如:二进制方式尾部写入文件 ios::binary | ios::ate | ios::out

代码示例: 

写文件简单代码示例:

#include "iostream"
#include "fstream"

using namespace std;

void test()
{
	std::ofstream ofs;
	ofs.open("test.txt",ios::out);

	ofs << "服了" << endl;
	ofs << "但不后悔" << endl;
	ofs.close();

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

读文件简单演示(四种方式):

  1.  按行读取
  2. 按行读取数组接收
  3. 按行读取string接收
  4. 单个字符读取

代码示例:

#include "iostream"
#include "fstream"
#include "string"

using namespace std;

void read_file_test()
{
	std::ifstream ifs;
	ifs.open("test.txt", ios::in);
	if(ifs.is_open())
	{
		cout << "文件打开成功" << endl;
		
	}
	else
	{
		cout << "文件打开失败" << endl;
		return;
	}
	//方式一 按行读取
	char buff1[1024] = { 0 };
	while (ifs >> buff1)
	{
		cout << buff1 << endl;
	}
	//方式二 
	char buff2[1024] = { 0 };
	while (ifs.getline(buff2, sizeof(buff2)))
	{
		cout << buff2 << endl;
	}
	//方式三 string

	string buff_str;
	while (getline(ifs, buff_str))
	{
		cout << buff_str << endl;
	}
	//方式四 读取单个字符效率低
	char c;
	while ((c = ifs.get()) != EOF)
	{
		cout << c;
	}
	ifs.close();

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

5.2二进制文件

-文件以二进制的形式存储在计算机中

5.2.1写文件

主要利用流对象调用对象函数write

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

buffer 指向内存中一段存储空间,len是写入的字节数

代码示例:

#include "iostream"
#include "fstream"
using namespace std;
using namespace std;
//二进制的形式不只可以操作基本类型也可以操作自定义类型
class dog
{
public:
	char m_name;
	int m_age;
public:
	dog()
	{
		m_name = 'A';
		m_age = 19;
	}
};
void write_file_binary()
{
	std::ofstream ofs;
	ofs.open("write_file_binary.txt", ios::out | ios::binary);
	dog d;
	string s = "无语";
	ofs << "无语" << endl;
	ofs.write((const char*)&d, sizeof(dog));
	ofs.write((const char*)&s, sizeof(s));
	ofs.close();
}
int main()
{

	write_file_binary();
	system("pause");
	return 0;
}

 运行结果:

5.2.2读文件

主要利用流对象调用对象函数read

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

buffer指向内存中一段存储空间,len是读的字节数。

代码示例:

#include "iostream"
#include "fstream"
using namespace std;
using namespace std;
//二进制的形式不只可以操作基本类型也可以操作自定义类型
class dog
{
public:
	char m_name;
	int m_age;
public:
	dog()
	{
		m_name = 'A';
		m_age = 19;
	}
};
void read_file_binary()
{
	std::ifstream ifs;
	ifs.open("write_file_binary.txt", ios::in | ios::binary);
	if (ifs.is_open())
	{
	}
	else
	{
		cout << "打开失败!" << endl;
	}
	dog d;
	ifs.read((char*)&d, sizeof(dog));
	cout << "d.name :" << d.m_name << "  " << "d.age :" << d.m_age << endl;
	ifs.close();
}
int main()
{

	read_file_binary();
	system("pause");
	return 0;
}

运行结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DQ小恐龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值