数据结构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();
相关推荐
Larry_Yanan7 小时前
Qt多进程(一)进程间通信概括
开发语言·c++·qt·学习
J ..8 小时前
C++ 多线程编程基础与 std::thread 使用
c++
你的冰西瓜8 小时前
C++标准模板库(STL)全面解析
开发语言·c++·stl
闻缺陷则喜何志丹8 小时前
【计算几何】仿射变换与齐次矩阵
c++·数学·算法·矩阵·计算几何
chen_ever8 小时前
Protobuf详解(从安装到实战)
c++·rpc·信息与通信
hmbbcsm8 小时前
python做题小记(八)
开发语言·c++·算法
再睡一夏就好9 小时前
深入Linux线程:从轻量级进程到双TCB架构
linux·运维·服务器·c++·学习·架构·线程
特立独行的猫a9 小时前
C++开发中的Pimpl机制与类声明机制深度解析:现代C++的编译解耦艺术
开发语言·c++·pimpl
GoWjw10 小时前
在C&C++指针的惯用方法
c语言·开发语言·c++
君义_noip10 小时前
信息学奥赛一本通 1453:移动玩具 | 洛谷 P4289 [HAOI2008] 移动玩具
c++·算法·信息学奥赛·csp-s