使用标准C / C ++检查文件是否存在的最佳方法
检查文件是否存在的唯一方法是尝试打开文件以进行读取或写入。
这是一个例子-
在C中
示例
#include<stdio.h>
int main() {
/* try to open file to read */
FILE *file;
if (file = fopen("a.txt", "r")) {
fclose(file);
printf("file exists");
} else {
printf("file doesn't exist");
}
}输出结果
file exists
在C++中
示例
#include <fstream>
#include<iostream>
using namespace std;
int main() {
/* try to open file to read */
ifstream ifile;
ifile.open("b.txt");
if(ifile) {
cout<<"file exists";
} else {
cout<<"file doesn't exist";
}
}输出结果
file doesn't exist
热门推荐
10 结婚祝福语简短精辟 兄弟
11 六十岁大寿祝福语简短
12 元旦联谊祝福语简短精辟
13 祝球员赛前祝福语简短
14 男朋友新年祝福语简短
15 20岁简短生日祝福语
16 国庆简短幽默祝福语
17 圆圆的祝福语简短
18 弟弟生日祝福语简短的话