数据结构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();
相关推荐
MZ_ZXD0017 分钟前
springboot音乐播放器系统-计算机毕业设计源码76317
java·c语言·c++·spring boot·python·flask·php
Emberone29 分钟前
C++ list 详解:从入门到模拟实现,彻底搞懂双向链表
c++·list
Cando学算法1 小时前
欧拉回路(一笔画)
数据结构·c++·图论
我不是懒洋洋1 小时前
手写一个并查集:从原理到最小生成树实战
c语言·c++·经验分享·算法
图码1 小时前
一文搞懂如何判断字符串是否为Pangram(全字母句)
数据结构·算法·网络安全·数字雕刻·ping++
叼烟扛炮1 小时前
C++ 知识点06 inline
开发语言·c++·inline
khalil10202 小时前
代码随想录算法训练营Day-43 动态规划10 | 300.最长递增子序列、674. 最长连续递增序列、718. 最长重复子数组
数据结构·c++·算法·leetcode·动态规划·子序列问题
ximu_polaris2 小时前
设计模式(C++)-行为型模式-解释器模式
c++·设计模式·解释器模式
2401_897190552 小时前
【C++高阶系列】告别内查找局限:基于磁盘 I/O 视角的 B 树深度剖析与 C++ 泛型实现!
java·c++·算法
多加点辣也没关系2 小时前
数据结构与算法|第十一章:跳表
数据结构·算法