C++检测一个文件是否存在

时间:2022-06-26 07:13:57

ifstream::is_open - C++ Reference

http://www.cplusplus.com/reference/fstream/ifstream/is_open/

 // ifstream::is_open
#include <iostream> // std::cout
#include <fstream> // std::ifstream int main () {
std::ifstream ifs ("test.txt"); if (ifs.is_open()) {
// print file:
char c = ifs.get();
while (ifs.good()) {
std::cout << c;
c = ifs.get();
}
}
else {
// show message:
std::cout << "Error opening file";
} return ;
}