数据结构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();
相关推荐
CN-Dust2 分钟前
【C++】while语句例题专题
数据结构·c++·算法
用户805533698035 分钟前
现代Qt开发教程(新手篇)1.11——定时器
c++·qt
澈20712 分钟前
STL迭代器:容器遍历的万能钥匙
开发语言·c++
azoo21 分钟前
emplace_back和push_back() 函数添加 cv::Point 类型数据
c++·opencv
样例过了就是过了1 小时前
LeetCode热题 不同路径
c++·算法·leetcode·动态规划
橙子也要努力变强2 小时前
信号的保存、阻塞与递达
linux·服务器·c++
旖-旎2 小时前
深搜练习(组合总和)(7)
c++·算法·深度优先·力扣
T0uken3 小时前
基于 vcpkg 与 LLVM-MinGW 的 Qt6 静态链接开发方案
c++·windows·qt
睡一觉就好了。3 小时前
C++11(一)
c++