C++入门新手村-文件读写知识讲解

文件读写知识讲解

C++简单文件操作

  • 文本文件 文件以文本的ASCII码形式存储在计算机中
  • 二进制文件 文件以文本的二进制形式存储在计算机中,用户一般不能直接读懂它们

操作文件的三大类:

  • ofstream:写操作
  • ifstream:读操作
  • fstream:读写操作

读文件

读文件的基本步骤如下:

①包含头文件 #include <fstream>

②创建流对象 ifstream ifs;

③打开文件并判断文件是否打开成功 ifs.open("file uri",打开方式);

④读数据 四种方式读取

⑤关闭文件 ifs.close()

读文本文件

cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	ifstream ifs;
	//打开文件
	ifs.open("C:/Users/darryl/Desktop/test.txt", ios::in);
	//判断文件是否打开
	if (ifs.is_open())
	{
		//读取文件四种方法
		//第一种读取方法
		/*char buf[1024] = { 0 };
		while (ifs >> buf)
		{
			std::cout << buf << std::endl;
		}*/
		//第二种读取方法
		/*char buf[1024] = { 0 };
		while (ifs.getline(buf,sizeof(buf)))
		{
			std::cout << buf << std::endl;
		}*/
		//第三种读取方法
		/*string buf;
		while (getline(ifs,buf))
		{
			std::cout << buf << std::endl;
		}*/
		//第四种读取方法
		char c;
		while ((c = ifs.get()) != EOF) //end of file 标记
		{
			std::cout << c;
		}
	}
	else
	{
		std::cout << "文件打开失败" << std::endl;
		return 0;
	}
	//关闭文件
	ifs.close();
	return 0;
}

读二进制文件

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

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

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

cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Person
{
private:
	//姓名
	string m_Name;
	//年龄
	int m_Age;
public:
	string getName()
	{
		return this->m_Name;
	}

	int getAge()
	{
		return this->m_Age;
	}
};

int main()
{
	ifstream ifs;
	ifs.open("C:/Users/darryl/Desktop/test,txt", ios::in | ios::binary);
	if (!ifs.is_open())
	{
		std::cout << "文件打开失败!" << std::endl;
		return 0;
	}
	Person person;
	ifs.read((char*)&person, sizeof(Person));
	std::cout << "姓名:" << person.getName() << " 年龄:" << person.getAge() << std::endl;
	ifs.close();
}

写文件

写文件的基本步骤如下:

①包含头文件 #include <fstream>

②创建流对象 ofstream ofs;

③打开文件 ofs.open("file uri");

④写数据 ofs << "写入的数据";

⑤关闭文件 ofs.close();

文件打开方式:

  • ios::in 为读文件而打开文件
  • ios::out 为写文件而打开文件
  • ios::ate 初始位置:文件尾
  • ios::app 追加方式写文件
  • ios::trunc 如果文件存在先删除再创建
  • ios::binary 二进制方式

打开文件方式可以配合使用,利用 | 操作符:ios::binary | ios::out

写文本文件

cpp 复制代码
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	//创建流对象
	ofstream ofs;
	//指定打开访问
	ofs.open("C:/Users/25763/Desktop/test.txt", ios::out);
	//写内容
	ofs << "姓名 : 张三" << std::endl;
	//关闭文件
	ofs.close();
}

写二进制文件

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

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

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

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

cpp 复制代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Person
{
private:
	//姓名
	string m_Name;
	//年龄
	int m_Age;
public:
	Person(string name, int age)
	{
		this->m_Age = age;
		this->m_Name = name;
	}
};

int main()
{
	ofstream ofs;
	ofs.open("C:/Users/darryl/Desktop/test,txt", ios::out | ios::binary);
	Person person = { "张三",12 };
	ofs.write((const char*)&person, sizeof(Person));
	ofs.close();
}
相关推荐
Mark127712 分钟前
Trie 树(字典树)
c++·mark1277
Jiezcode37 分钟前
Unreal Engine ClassName Rule
c++·游戏·图形渲染·虚幻引擎
我爱学嵌入式1 小时前
C语言:第18天笔记
c语言·开发语言·笔记
long_run1 小时前
C++之静态库 vs 动态库
c++
楚Y6同学2 小时前
QT之QSS的使用方法和常用控件的样式设置
开发语言·qt
NekoCNN2 小时前
流式处理-与迭代相对的现代编程范式
编程语言
Yolo566Q3 小时前
“R语言+遥感”的水环境综合评价方法实践技术应用
开发语言·r语言
文火冰糖的硅基工坊5 小时前
[激光原理与应用-317]:光学设计 - Solidworks - 草图
开发语言·python·信息可视化·系统架构
草莓熊Lotso5 小时前
【C语言强化训练16天】--从基础到进阶的蜕变之旅:Day10
c语言·开发语言·经验分享·算法·强化
草明5 小时前
docker stats 增加一列容器名称的显示
java·开发语言·docker