数据结构9.3 - 文件基础(C++)

目录

上图源自:https://blog.csdn.net/LG1259156776/article/details/47035583

1 打开文件

法 1 法 2
ofstream file(path); ofstream file; file.open(path);
cpp 复制代码
#include<bits/stdc++.h>
using namespace std;

int main()
{
	char path[] = "./test.txt";
	//ofstream file(path); // 法1 
	ofstream file;
	file.open(path); // 法2 
	if(!file)
	{
		cout << "can not open the " << path + 2 << "!" <<endl;
		exit(0);
	}
	else
		cout << path + 2 << " has been opened!" << endl << endl;
	file.close();
	
	system("pause");
	return 0;
}

字符读写

关闭文件

cpp 复制代码
	file.close();
相关推荐
hnjzsyjyj5 小时前
东方博宜OJ 2190:树的重心 ← 邻接表 or 链式前向星
数据结构·链式前向星·树的重心
ChoSeitaku7 小时前
NO15数据结构选择题考点|线性表|栈和队列|串
数据结构
hetao17338378 小时前
2025-12-12~14 hetao1733837的刷题笔记
数据结构·c++·笔记·算法
椰子今天很可爱8 小时前
五种I/O模型与多路转接
linux·c语言·c++
一直都在5728 小时前
数据结构入门:时间复杂度与排序和查找
数据结构
程序员zgh9 小时前
C++ 互斥锁、读写锁、原子操作、条件变量
c语言·开发语言·jvm·c++
sin_hielo11 小时前
leetcode 2110
数据结构·算法·leetcode
panzer_maus11 小时前
归并排序的简单介绍
java·数据结构·算法
獭.獭.11 小时前
C++ -- STL【unordered_set和unordered_map的使用】
c++·stl·unordered_map·unordered_set
摆烂且佛系11 小时前
B+树的“页分裂“机制
数据结构·b树