
检查文件是否存在的唯一方法是尝试以读取或写入的方式打开文件。
下面是一个示例:
在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");
}
}</stdio.h>
输出
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<h2>输出</h2>
<pre class="result notransalte">file doesn't exist
C++免费学习笔记(深入):立即使用
在学习笔记中,你将探索 C++ 的入门与实战技巧!











