数据结构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();
相关推荐
轩情吖几秒前
MySQL内置函数
android·数据库·c++·后端·mysql·开发·函数
AC__dream9 分钟前
2024秋招-字节跳动-算法岗笔试
数据结构·算法
一叶落43815 分钟前
LeetCode 151. 反转字符串中的单词(C语言)【双指针 + 字符串处理】
c语言·数据结构·算法·leetcode
wangjialelele16 分钟前
详解Redis终端操作和Redis-plus-plus接口使用
linux·数据库·c++·redis·分布式·缓存·中间件
junnhwan17 分钟前
LeetCode Hot 100——栈
java·数据结构·算法·leetcode·hot 100
hyl2001219 分钟前
c++ SCIP求解整数规划模型
开发语言·c++
sqyno1sky20 分钟前
代码动态生成技术
开发语言·c++·算法
superior tigre21 分钟前
347 前k个高频元素
数据结构·算法·leetcode
2401_8535765025 分钟前
C++中的策略模式变体
开发语言·c++·算法
学嵌入式的小杨同学28 分钟前
STM32 进阶封神之路(十四):语音交互实战 ——SU03T 语音识别模块从固件制作到 STM32 控制(串口通信 + 命令响应)
c++·stm32·单片机·嵌入式硬件·架构·硬件架构·ux