数据结构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();
相关推荐
冠位观测者6 分钟前
【Leetcode 每日一题 - 扩展】1512. 好数对的数目
数据结构·算法·leetcode
_Itachi__8 分钟前
LeetCode 热题 100 560. 和为 K 的子数组
数据结构·算法·leetcode
Abdullah al-Sa11 分钟前
Docker教程(喂饭级!)
c++·人工智能·docker·容器
进击的_鹏13 分钟前
【C++】list 链表的使用+模拟实现
开发语言·c++·链表
大模型铲屎官38 分钟前
哈希表入门到精通:从原理到 Python 实现全解析
开发语言·数据结构·python·算法·哈希算法·哈希表
C语言扫地僧1 小时前
RPC 框架项目剖析
c++·网络协议·学习·rpc
L_09071 小时前
【C】队列与栈的相互转换
c语言·开发语言·数据结构
苍老流年1 小时前
Redis底层数据结构
数据结构·数据库·redis
水月梦镜花2 小时前
数据结构:基数排序(c++实现)
开发语言·数据结构·c++
愈谦卑2 小时前
数据结构:排序
数据结构·算法·排序算法