数据结构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();
相关推荐
风筝在晴天搁浅12 分钟前
hot100 234.回文链表
数据结构·链表
superman超哥13 分钟前
仓颉类型别名的使用方法深度解析
c语言·开发语言·c++·python·仓颉
superman超哥1 小时前
仓颉GC调优参数深度解析
c语言·开发语言·c++·python·仓颉
誰能久伴不乏1 小时前
Linux `epoll` 学习笔记:从原理到正确写法(含 ET 经典坑总结)
linux·服务器·网络·c++·ubuntu
(❁´◡`❁)Jimmy(❁´◡`❁)1 小时前
【算法】 二分图理论知识和判断方法
c++·算法
徐子童1 小时前
优选算法---哈希表
数据结构·算法·哈希表
im_AMBER1 小时前
Leetcode 85 【滑动窗口(不定长)】最多 K 个重复元素的最长子数组
c++·笔记·学习·算法·leetcode·哈希算法
B_lack0261 小时前
字节转换算法应用_读取本地时间
数据结构·算法·数组·西门子plc·博途·时间处理·scl
leiming62 小时前
c++ string 容器
开发语言·c++·算法
superman超哥2 小时前
仓颉Option类型的空安全处理深度解析
c语言·开发语言·c++·python·仓颉