파일이 존재하는지 확인하는 유일한 방법은 읽기 또는 쓰기를 위해 파일을 열어보는 것입니다.
다음은 예입니다 -
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