目录
上图源自: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();