数据结构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();
相关推荐
寻寻觅觅☆几秒前
东华OJ-基础题-30-求最晚和最早日期(C++)
数据结构·c++·算法
_Twink1e1 小时前
[算法竞赛]九、C++标准模板库STL常用容器大全
开发语言·c++
bu_shuo1 小时前
c++中对数组求和
开发语言·c++
elseif1231 小时前
【Markdown】指南(上)
linux·开发语言·前端·javascript·c++·笔记
星辰徐哥2 小时前
C++网络编程:TCP服务器与客户端的实现
网络·c++·tcp/ip
·心猿意码·2 小时前
C++ volatile 与 std::atomic 底层语义剖析
c++
Tanecious.2 小时前
蓝桥杯备赛:Day1-奖学金
c语言·c++·蓝桥杯
汉克老师2 小时前
GESP2025年6月认证C++三级( 第三部分编程题(1、奇偶校验)
c++·算法·gesp三级·gesp3级·按位操作
CoderCodingNo2 小时前
【GESP】C++八级考试大纲知识点梳理 (8) 算法优化技巧
开发语言·c++
We་ct2 小时前
LeetCode 295. 数据流的中位数:双堆解法实战解析
开发语言·前端·数据结构·算法·leetcode·typescript·数据流