doc/docs/CPlusPlus-main/27_C++的文件操作.md
sairate fa9377e4ae docs(book): 添加现代 C++教程及相关代码
- 新增现代 C++ 教程的 Preface 章节,包括英文和中文版本
- 添加 C++ Primer 练习代码
- 新增 Learn C++ 教程的 C++ 开发简介章节
- 添加头文件解析文档
- 更新 mkdocs.yml,包含新教程的目录结构
- 修改项目设置,使用 Python 3.10环境
2025-07-08 09:52:45 +08:00

294 lines
6.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# C++的文件操作
# 1. 文件操作简介
① 程序运行时产生的数据都属于临时数据,程序一旦运行结束都会被释放。
② 通过文件可以将数据持久化。
③ C++中对文件操作需要包含头文件<fstream>
④ 文件类型分为两种:
1. 文本文件文件以文本的ASCII码形式存储在计算机中。
2. 二进制文件:文件以文本的二进制形式存储在计算机中,用户一般不能直接读懂它们。
⑤ 操作文件的三大类
1. ofstream写操作
2. ifstream读操作
3. fstream读写操作
⑥ 文件打开方式可以配合使用,利用|操作符。
1. 例如,用二进制方式写文件 ios:: binary | ios:: out
![image.png](27_C++的文件操作_files/image.png)
# 2. 文本文件--写文件
① 写文件步骤如下:
1. 包含头文件 #include $<fstream>$
2. 创建流对象 ofstream ofs;
3. 打开文件 ofs.open("文件路径",打开方式);
4. 关闭文件 ofs<<"写入数据";
5. 关闭文件 ofs.close();
```python
#include <iostream>
using namespace std;
#include<fstream> //头文件包含
//文本文件 写文件
void test01()
{
//1包含头文件 fstream
//2创建流文件
ofstream ofs; //创建流对象
//3指定打开方式
ofs.open("test.txt", ios::out); //当没有指定某盘路径时创建的文件在该项目文件的路径下
//4写内容
ofs << "姓名:张三" << endl;
ofs << "性别:男" << endl;
ofs << "年龄18" << endl;
//5关闭文件
ofs.close();
}
int main()
{
test01();
system("pause");
return 0;
}
```
运行结果:
![image.png](27_C++的文件操作_files/image.png)
# 3. 文本文件--读操作
① 读文件与写文件步骤相似,但是读取方式相对比较多。
② 读文件步骤如下:
1. 包含头文件 #include$<fstream>$
2. 创建流对象 ifstream ifs;
3. 打开文件并判断文件是否成功打开 ifs.open("文件路径",打开方式);
4. 四种方式读取
5. 关闭文件 ifs.close();
```python
#include <iostream>
using namespace std;
#include<fstream>
#include<string>
//文本文件 读文件
void test01()
{
//1包含头文件 fstream
//2创建流文件
ifstream ifs; //创建流对象
//3打开文件并且判断是否打开成功
ifs.open("test.txt", ios::in);
if (!ifs.is_open())
{
cout << "文件打开失败" << endl;
return;
}
//4读数据
/*
//第一种
char buf[1024] = { 0 }; //字符数组初始化全为0
while (ifs >> buf) //一行一行数据读存到buf里当读不到数据的时候返回False退出循环
{
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;
}
*/
//第四种
char c;
while ((c = ifs.get()) != EOF) //每一次读取一个字符,字符放入c
{ //EOF表示读取到文件尾
cout << c;
}
//5关闭文件
ifs.close();
}
int main()
{
test01();
system("pause");
return 0;
}
```
运行结果:
- 姓名:张三
- 性别:男
- 年龄18
- 请按任意键继续. . .
# 4. 二进制文件--写文件
① 以二进制的方式对文件进行读、写操作,打开方式要指定为 ios::binary
② 二进制方式写文件主要利用流对象调用成员函数write函数原型ostream write(const char * buffer,int len)
③ 参数解释字符指针buffer指向内存中一段内存空间len是读写的字节数。
```python
#include <iostream>
using namespace std;
#include<fstream>
//二进制文件 写文件
class Person
{
public:
char m_Name[64]; //姓名
int m_Age; //年龄
//写字符串的时候最好不要用C++的string有可能出现问题最好用C的数组写字符串因为它底层是用C写的
};
void test01()
{
//1包含头文件
//2创建流文件
ofstream ofs;
//3打开文件
ofs.open("person.txt", ios::out | ios::binary); //二进制的方式写文件
//ofstream ofs.open("person.txt",ios::out | ios::binary) 也可以两步合成一步
//4写文件
Person p = { "张三",18 };
ofs.write((const char*)&p, sizeof(Person)); //对p取地址然后强转为常量指针
//5关闭文件
ofs.close();
}
int main()
{
test01();
system("pause");
return 0;
}
```
![image.png](27_C++的文件操作_files/image.png)
# 5. 二进制文件--读文件
① 二进制方式读文件主要利用流对象调用成员函数read。
② 函数原型istream& read(char *buffer,int len);
③ 参数解释字符串buffer指向内存中一段内存空间len是读写的字节数。
```python
#include <iostream>
using namespace std;
#include<fstream>
//二进制文件 读文件
class Person
{
public:
char m_Name[64]; //姓名
int m_Age; //年龄
//写字符串的时候最好不要用C++的string有可能出现问题最好用C的数组写字符串因为它底层是用C写的
};
void test01()
{
//1包含头文件
//2创建流对象
ifstream ifs;
//3打开文件
ifs.open("person.txt", ios::in | ios::binary); //二进制的方式读文件
if (!ifs.is_open())
{
cout << "文件打开失败" << endl;
return;
}
//4写文件
Person p;
ifs.read((char*)&p, sizeof(Person));
cout << "姓名:" << p.m_Name << " 年龄:" << p.m_Name << endl;
//5关闭文件
ifs.close();
}
int main()
{
test01();
system("pause");
return 0;
}
```
运行结果:
- 姓名:张三 年龄:张三
- 请按任意键继续. . .