C++核心高级编程 --- 5.文件操作

文章目录

    • 第五章:
      • 5.文件操作
        • [5.1 文本文件](#5.1 文本文件)
          • [5.1.1 写文件](#5.1.1 写文件)
          • [5.1.2 读文件](#5.1.2 读文件)
        • [5.2 二进制文件](#5.2 二进制文件)
          • [5.2.1 写文件](#5.2.1 写文件)
          • [5.2.2 读文件](#5.2.2 读文件)

第五章:

5.文件操作

作用 :程序运行时产生的数据都是临时数据,程序运行结束后都会被释放,文件能将数据持久化

对文件进行操作需要包含头文件

两种文件类型

  • 文本文件:以文本ASCII码形式存储在计算机里

  • 二进制文件:以文本二进制形式存储在计算机里

操作文件的三大类:

  • ofstream 写操作

  • ifstream 读操作

  • fstream 读写操作

5.1 文本文件
5.1.1 写文件

步骤

  1. 包含头文件:#include

  2. 创建流对象:ofstream ofs;

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

  4. 写入数据:ofs << "写入的数据"

  5. 关闭文件:ofs.close();

文件打开方式:

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

文件打开方式可配合使用,用|操作符即可。

如用二进制方式写文件:ios::binary | ios::out

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

int main()
{
	ofstream ofs;
	ofs.open("text.txt", ios::out);
	ofs << "hello world" << endl;
	ofs << "hello C++" << endl;
	ofs.close();
	system("pause");
	return 0;
}
5.1.2 读文件

步骤

  1. 包含头文件:#include

  2. 创建流对象:ifstream ifs;

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

  4. 读取数据:四种方式,代码中会说明

  5. 关闭文件:ifs.close();

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

int main()
{
	ifstream ifs;
	ifs.open("text.txt", ios::in);
	if (!ifs.is_open())  //判断文件是否打开成功
	{
		cout << "打开文件失败!" << endl;
		return 0;
	}

	//读取数据
	//第一种方式
	/*char buf[1024] = { 0 };
	while (ifs >> buf)
	{
		cout << buf << endl;
	}
	system("pause");*/
	
	//第二种方式
	/*char buf[1024] = { 0 };
	while (ifs.getline(buf, sizeof(buf)))
	{
		cout << buf << endl;
	}*/

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


	//第四种方式
	char c;
	while ((c = ifs.get()) != EOF)
	{
		cout << c;
	}

	ifs.close();
	return 0;
}
5.2 二进制文件

打开方式指定:ios::binary

5.2.1 写文件

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

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

说明:字符指针buffer指向内存中一段存储空间。len是读写的字节数。

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

class Student
{
public:
	char name[20];
	int age;
};

int main()
{
	ofstream ofs("stuInfo.txt", ios::out | ios::binary);
	Student s = { "小明", 18 };
	ofs.write((const char*)&s, sizeof(Student));
	ofs.close();
	system("pause");
	return 0;
}
5.2.2 读文件

二进制方式读文件可用流对象调用成员函数read

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

说明:字符指针buffer指向内存中一段存储空间。len是读写的字节数。

c++ 复制代码
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class Student
{
public:
	char name[20] ;
	int age;
};

int main()
{
	ifstream ifs;
	ifs.open("stuInfo.txt", ios::binary | ios::in);
	if (!ifs.is_open())
	{
		cout << "打开文件失败!" << endl;
		return 0;
	}
	Student s;
	ifs.read((char*)&s, sizeof(s));
	cout << "姓名:" << s.name << " 年龄:" << s.age << endl;
	ifs.close();
	system("system");
	return 0;
f(s));
	cout << "姓名:" << s.name << " 年龄:" << s.age << endl;
	ifs.close();
	system("system");
	return 0;
}
相关推荐
意疏8 分钟前
【Python篇】PyCharm 安装与基础配置指南
开发语言·python·pycharm
看到我,请让我去学习1 小时前
OpenCV编程- (图像基础处理:噪声、滤波、直方图与边缘检测)
c语言·c++·人工智能·opencv·计算机视觉
GuokLiu1 小时前
250708-通过两块硬盘拷贝DeepSeek两个满血版模型的bash脚本
开发语言·chrome·bash
iCxhust8 小时前
c# U盘映像生成工具
开发语言·单片机·c#
yangzhi_emo8 小时前
ES6笔记2
开发语言·前端·javascript
emplace_back9 小时前
C# 集合表达式和展开运算符 (..) 详解
开发语言·windows·c#
jz_ddk9 小时前
[学习] C语言数学库函数背后的故事:`double erf(double x)`
c语言·开发语言·学习
萧曵 丶10 小时前
Rust 所有权系统:深入浅出指南
开发语言·后端·rust
xiaolang_8616_wjl10 小时前
c++文字游戏_闯关打怪2.0(开源)
开发语言·c++·开源