数据结构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();
相关推荐
小张成长计划..1 小时前
【C++】2:cin和cout的介绍和使用,函数的缺省参数
c++
再卷也是菜1 小时前
C++篇(17)哈希拓展学习
c++·哈希
“愿你如星辰如月”1 小时前
Linux:进程间通信
linux·运维·服务器·c++·操作系统
R-G-B2 小时前
归并排序 (BM20 数组中的逆序对)
数据结构·算法·排序算法
少许极端2 小时前
算法奇妙屋(十二)-优先级队列(堆)
数据结构·算法·leetcode·优先级队列··图解算法
灵晔君2 小时前
C++标准模板库(STL)——list的模拟实现
c++·list
kupeThinkPoem3 小时前
哈希表有哪些算法?
数据结构·算法
爬山算法3 小时前
Redis(127)Redis的内部数据结构是什么?
数据结构·数据库·redis
Justinyh3 小时前
1、CUDA 编程基础
c++·人工智能
white-persist4 小时前
差异功能定位解析:C语言与C++(区别在哪里?)
java·c语言·开发语言·网络·c++·安全·信息可视化