数据结构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();
相关推荐
MMjeaty27 分钟前
查找及其算法
c++·算法
寂静山林1 小时前
UVa 1597 Searching the Web
数据结构·算法
yong15858553431 小时前
1. Linux C++ muduo 库学习——库的编译安装
linux·c++·学习
952362 小时前
数据结构-顺序表
java·数据结构·学习
mit6.8242 小时前
回溯剪枝trick
c++
haofafa3 小时前
高精度加减法
java·数据结构·算法
渡我白衣3 小时前
C++世界的混沌边界:undefined_behavior
java·开发语言·c++·人工智能·深度学习·语言模型
却道天凉_好个秋3 小时前
c++ 协程
c++
QQ12958455044 小时前
ThingsBoard部件数据结构解析
数据结构·数据库·物联网·iot
chian-ocean4 小时前
双向链表的“链”与“殇”——Rust LinkedList 的深度剖析、实战与再思考
数据结构·链表·rust