Updated April 4, 2023
Introduction to C++ file exists
Whenever we want to do some tasks like performing a backup of a file or modifying something in the file or copying something from the file, or simply reading the file from the file, we have to first make sure the file exists. Unfortunately, there is no built-in function as such to check for the existence of the file. However, we can come up with a function in C++ using the available built-in functions to check for the file’s existence, which returns true in case the file exists and returns false in case the file does not exist, and this file exists is a Boolean function in C++.
The syntax for file exists function in C++ is as follows:
bool FileExists(char *pathofthefile)
where pathofthefile represents the path in which the file is located whose existence needs to be verified before taking any actions.
Working of the file exist in C++
- Whenever we want to do some tasks like performing a backup of a file or modifying something in the file or copying something from the file, or simply reading the file from the file, we have to first make sure the file exists.
- But there is no built-in function as such to check for the existence of the file.
- However, we can come up with a function in C++ using the available built-in functions to check for the existence of the file, and this function is called the file exists function in C++.
- File exists function is a Boolean function in C++ that returns true if the file exists and returns false if the file does not exist.
Examples of C++ files exist
Given below are the examples of C++ files exist:
Example #1
C++ program to demonstrate File Exists function to check if the file at a given location exists or not and returns true if the file exists or returns false if the file do not exist:
Code:
// two header files iostream and fstream is included to enable us to use cout and ifstream. #include <iostream>
#include <fstream>
using namespace std;
//defining the file exists function which checks if a file exists or not and returns one if file exists and returns 0 if file do not exist
bool FileExists(string filename)
{
ifstream file(filename);
if(file.is_open())
{
return 1;
file.close();
}
else
{
return 0;
}
}
//main method is called
int main ()
{
//creating a variable to store the input provided by the user
char input[10];
ifstream ifile;
//creating a new file to store the data provided by the user
ifile.open("new.txt");
while (!ifile.eof())
{
ifile.getline(input, 10);
cout << input << endl;
}
ifile.close();
//calling the file exists function to check if the file exists or no
cout << FileExists("new.txt") << endl;
return 0;
}
Output:
In the above program, two header files, iostream and stream, are included to use cout and ifstream. Then the file exists function is defined, which checks if the file exists or not and returns one if the file exists and returns 0 if the file does not exist. Then the main method is called, within which a variable is defined to store the data provided by the user. A file is then created, and the data provided by the user is written into the file, and the file is closed. The file is created for the purpose of checking the existence of the file. Then the file exists function is called, which returns a 1 if the file exists and returns a 0 if the file does not exist.
Example #2
C++ program to demonstrate File Exists function to check if the file at a given location exists or not and returns true if the file exists or returns false if the file do not exist:
Code:
// two header files iostream and fstream is included to enable us to use cout and ifstream.
#include <iostream>
#include <fstream>
using namespace std;
//defining the file exists function which checks if file exists or not and returns one if file exists and returns 0 if file do not exist
bool FileExists(string filename)
{
ifstream file(filename);
if(file.is_open())
{
return 1;
file.close();
}
else
{
return 0;
}
}
//main method is called
int main ()
{
//creating a variable to store the input provided by the user
char input[10];
ifstream ifile;
//creating a new file to store the data provided by the user
ifile.open("new1.txt");
while (!ifile.eof())
{
ifile.getline(input, 10);
cout << input << endl;
}
ifile.close();
//calling the file exists function to check if the file exists or no
cout << FileExists("new1.txt") << endl;
return 0;
}
Output:
In the above program, two header files, iostream and stream, are included to use cout and ifstream. Then the file exists function is defined, which checks if a file exists or not and returns one if the file exists and returns 0 if the file does not exist. Then the main method is called, within which a variable is defined to store the data provided by the user. A file is then created, and the data provided by the user is written into the file, and the file is closed. The file is created for the purpose of checking the existence of the file. Then the file exists function is called, which returns a 1 if the file exists and returns a 0 if the file does not exist.
Conclusion
In this tutorial, we understand the concept of the file exists function in C++ through definition, the syntax of a file exists function, and the working of a file exists function through programming examples and their outputs.
Recommended Articles
This is a guide to the C++ file exists. Here we discuss the Working of the file exists in C++ is and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –