数据结构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();
相关推荐
AI视觉网奇1 天前
Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr
开发语言·c++·算法
智者知已应修善业1 天前
【输入两个数字,判断两数相乘是否等于各自逆序数相乘】2023-10-24
c语言·c++·经验分享·笔记·算法·1024程序员节
oioihoii1 天前
C++11到C++23语法糖万字详解
java·c++·c++23
比昨天多敲两行1 天前
C++入门基础
开发语言·c++
集3041 天前
C++多线程学习笔记
c++·笔记·学习
ComputerInBook1 天前
C++编程语言:标准库:第39章——本地化(语言环境)( Locales)(Bjarne Stroustrup)
c++·c++语言环境·c++ 本地化设置·c++ locale·c++ facet·语言特征
hefaxiang1 天前
分支循环(下)(二)
c语言·开发语言·数据结构
繁华似锦respect1 天前
C++ 智能指针底层实现深度解析
linux·开发语言·c++·设计模式·代理模式
Bona Sun1 天前
单片机手搓掌上游戏机(二十三)—esp32运行简单街机模拟器软硬件准备
c语言·c++·单片机
@小码农1 天前
2025年北京海淀区中小学生信息学竞赛第二赛段C++真题
开发语言·数据结构·c++·算法