फ़ाइल मौजूद है या नहीं यह जाँचने का एकमात्र तरीका फ़ाइल को पढ़ने या लिखने के लिए खोलने का प्रयास करना है।
यहाँ एक उदाहरण है -
सी में
उदाहरण
#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