数据结构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();
相关推荐
郝学胜-神的一滴5 小时前
Qt 高级开发 009: C++ Lambda 表达式
开发语言·c++·qt·软件构建
石山代码6 小时前
C++ 轻量级日志系统
开发语言·c++
smj2302_796826528 小时前
解决leetcode第3943题递增后的数对数量
数据结构·python·算法·leetcode
王老师青少年编程9 小时前
2026年全国青少年信息素养大赛初赛真题(算法应用主题赛C++初中组初赛真题3:文末附答案和解析)
c++·真题·答案·初赛·2026年·青少年信息素养大赛·初中组
轻颂呀9 小时前
C++11——并发库介绍
开发语言·c++
梓䈑10 小时前
【算法题攻略】快速排序 和 归并排序
数据结构·c++·排序算法
fan_music11 小时前
设计模式学习
c++·设计模式
小小编程路12 小时前
C++ 常用逻辑运算符
开发语言·c++·算法
he___H12 小时前
leetcode100-普通数组
java·数据结构·算法·leetcode
不知名的老吴13 小时前
经典算法实战:重新排列日志文件(二)
数据结构·算法