打开关闭文件并判断文件是否存在
题目内容:
编写程序
构造两个path对象,分别代表文件out.txt和in.txt;
用ofstream的构造函数打开文件out.txt;
用ifstream的open函数打开文件in.txt;程序结束前用close关闭该流
判断in.txt是否存在,若存在则输出“in.txt exists”,否则输出“in.txt does not exist”
输入格式:
无
输出格式:
无
输入样例:
无
输出样例:
无
#include <iostream>
#include <filesystem>
#include <fstream>
using namespace std;
int main() {
filesystem::path in("in.txt"), out("out.txt");
ofstream outfile(out);
ifstream infile;
infile.open(in);
if (infile) {
cout << "in.txt exists" << endl;
}
else {
cout << "in.txt does not exist" << endl;
}
infile.close();
system("pause");
}