C++文件操作完全指南:从文本读写到二进制文件处理

文本文件

1.文本文件 ASCII码

2,二进制文件 二进制存

操作文件三大类

1.ofstream写操作

2.ifstream读文件

3.fstream 读写文件

写文件

1.#include <fstream

2.创建流对象

ofstream ofs;

3.打开文件

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

4.写数据

ofs<<"写入的数据";

5.关闭文件

ofs.close();

文件打开方式

ios::in 以只读方式打开文件(只能读,不能写)

ios::out 以只写方式打开文件→ 会清空原有内容

ios::app 以追加方式写文件→ 不会清空,写在文件末尾

ios::ate 打开文件后,定位到文件末尾(但可以修改前面内容)

ios::trunc 打开时清空文件内容(默认和 out 一起生效)

ios::binary 以二进制模式打开(不做文本转换)

javascript 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<fstream>
using namespace std;
void test01(){
	//1.包含头文件
	//2.创建流对象
	ofstream ofs;
	//3.指定打开方式
	ofs.open("test.txt", ios::out);
	//4.写内容
	ofs << "姓名:张三" << endl;
	ofs << "年龄:18" << endl;
	ofs << "性别:男" << endl;
	//5.关闭
	ofs.close();
}
int main() {
	test01();
}

读文件

1.包含头文件

#include<fstream

2.创建流对象

ifstream ifs;

3.打开文件并判断文件是否打开成功

ifs.open("文件路径",打开方式);

4.读数据

四种方式读取

5.关闭文件

ifs.close()

javascript 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void test01(){
	//1.包含头文件
	//2.创建流对象
	ifstream ifs;
	//3.打开文件 并且判断是否打开成功
	ifs.open("test.txt", ios::in);

	if (!ifs.is_open()) {
		cout << "文件打开失败" << endl;
		return;
	}
	//4.读数据
	//第一种
	 char buf[1024] = { 0 };
	while (ifs >> buf) {
		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;
		//getline结束会返回false或者EOF
	}
	//第四种
	char c;
	while ((c = ifs.get()) != EOF) {
		cout << c;
	}

	//5.关闭文件
	ifs.close();
}
int main() {
	test01();
}

用二进制写文件

javascript 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class person {
public:
	char m_name[100];
	int m_age;
};
void test01(){
	ofstream ofs;
	ofs.open("text.txt", ios::out | ios::binary);
	person p = { "张三",18 };
	ofs.write((const char*)&p, sizeof(p));
	ofs.close();

}
int main() {
	test01();
}

用二进制读文件

javascript 复制代码
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class person {
public:
	char m_name[100];
	int m_age;
};
void test01(){
	ifstream ifs;
	ifs.open("test.txt", ios::in | ios::binary);
	if (!ifs.is_open()) {
		cout << "文件打开失败" << endl;
		return;
	}
	person p;
	ifs.read((char*)&p, sizeof(person));
	cout << "姓名" << p.m_name << endl;
	cout << "年龄" << p.m_age << endl;
	ifs.close();
}
int main() {
	test01();
}
相关推荐
坚果派·白晓明2 小时前
【鸿蒙PC】SDL3 适配:AtomCode + Skills 快速集成 NAPI 测试工具
c++·华为·ai编程·harmonyos·atomcode
huangdong_2 小时前
淘宝商品SKU图自动分类技术深度解析:从DOM解析到智能归档
开发语言·javascript·ecmascript
阿正的梦工坊2 小时前
【Rust】12-借用检查器与非词法生命周期
开发语言·后端·rust
qq_2518364573 小时前
基于java Web网络订餐系统设计与实现 源码文档
java·开发语言·前端
秋93 小时前
3年经验Python后端转AI Engineer:3个月实战转型计划(2026版)
开发语言·人工智能·python
凡人叶枫3 小时前
Effective C++ 条款17:以独立语句将 newed 对象置入智能指针
java·linux·开发语言·c++·算法
飞天狗1113 小时前
零基础JavaWeb入门——第2课:让网页“活”起来 —— JSP是什么?
java·开发语言·前端·后端·web
醇氧4 小时前
【Linux】Java 服务生产级部署指南:实现常驻后台、开机自启与系统服务化管理
java·开发语言
凡人叶枫4 小时前
Effective C++ 条款16:成对使用 new 和 delete 时要采取相同形式
开发语言·c++·effective c++