数据结构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();
相关推荐
沉淀的.晴天3 分钟前
FreeRTOS信号量
数据结构·算法
青山木34 分钟前
Hot 100 --- 打家劫舍
java·数据结构·算法·leetcode·动态规划
云边有个稻草人39 分钟前
Tera Term 鸿蒙 PC 适配全记录:用 ArkUI 与 Native C++ 重建多协议终端工作流
c++·stm32·harmonyos
牛油果子哥q40 分钟前
多模态大模型工程入门:图文Embedding、图文RAG、图片解析、C++多模态接口封装实战
开发语言·c++·embedding
6Hzlia2 小时前
【Classic 150 刷题计划】 LeetCode 134. 加油站 | C++ 贪心算法与局部否决逻辑
c++·leetcode·贪心算法
夜不会漫长2 小时前
C++:类和对象(1)
java·开发语言·c++
YSL0701242 小时前
线性表和顺序表
数据结构
6Hzlia2 小时前
【Classic 150 刷题计划】 LeetCode 274. H 指数 | C++ 二分答案法 (单调性降维打击)
c++·算法·leetcode
huang5791472 小时前
数据结构的抽象化与接口复用设计理念4
数据结构
动词ing2 小时前
【学习笔记】数据结构(链表合并 双指针合并有序链表)
数据结构·笔记·学习