C++

C++ Check If File Exists

Files are referred to save the data forever. In C++, file handling is a method to store the outcome of a code in the file and assists to execute different operations on it. The syntax to open the file is “.open (filename, mode)”. The current method is taking a track as an argument and then it returns the Boolean value with true, if it resembles an existing file or directory.

Here, we used some of the following examples: we initialize a variable for our file with the filenames to check them in the file system with existing functions. Keep in mind that the current method can only check the existing directory where the executable file is placed. If we want to check the file, the only logic is to select the file and must open the selected file then you can read or write something in this file.

The file signifies a storage intermediate for storing the data or the information. Streams refer to a structure of bytes. The file system where we save data that is in the form of text as well as in the form of binary is forever and we use this data when we need to read as well as to write in these files in the form of input and output processes with the help of shifting bytes. In this article, we are going to discuss different methods of checking if files exist in the memory with the help of a very simple method in C++. For running the codes, utilize the ‘DEV C++’ compiler.

Use ifile.open():

ifile.open() is mainly used to check if a file exists in the specific directory or not. In the filing, a stream refers to an abstract that signifies a method where input as well as output processes are executed. “ifile.open()” takes one argument that is the name of the file. “path” characterizes the path of the file in the below example. Look at the examples that display the method to check if the file is present in C++. In these examples, Ifstream is a data type that characterizes the input file and it is also used to read data from these files.

#include <fstream>

#include<iostream>

using namespace std;
int main() {
ifstreammyfile;
myfile.open("Untitled1.cpp");
myf(ifile) {
cout<<"file exists";
   } else {
cout<<"file doesn't exist";
   }
}

We utilize header files ‘#include <fstream.h>’ and ‘#include <iostream.h>’ that means file stream and input-output stream. After the namespace, we entered into the main body and we declared a variable for storing the file with the name of myfile. Then, we use the open function to open the file and pass the parameters having file name as well as extension. After that, we use the if statement to check whether the file exists or not. In the end, we display the message that depends on the statement.

We check the file using a file that does not reside in the system’s directory.

We can see the result below.

Use fopen():

A stream that can be signified as a basis or endpoint of the character of unspecified distance. “fstream” is a data type that signifies the file stream normally, and has the abilities of both ofstream and ifstream. This means it can create files, write data in the files, and also read the information from the files. When you work with the files, you work with the directory, the storage, and file and directory names.

A stream is an arrangement of the bytes that we can custom to read from the files, which can be several storage mediums. Fopen() is used for both to open as well as to generate a file. Opens the file that has a name, specified in the parameter where filename and links it with a stream that can be recognized in future processes by the FILE pointer. The processes that are acceptable on the stream and the way these are achieved are well-defined by the mode parameter.

#include <fstream>

#include<iostream>

using namespace std;
int main() {
   FILE *codefile;
   if(codefile=fopen("Untitled1.cpp","r")) {
cout<<"file exists";
fclose(codefile);
   } else {
cout<<"file doesn't exist";
   }
}

Use header files <fstream.h> and <iostream.h>. After that, we write the namespace then we enter in the body of the main and we declared a variable for saving the file. The name of the variable is a code file. Here, we use the fopen() function to open the file and pass the parameters having file name as well as extension

For the checking complete code and else statement, we get a file that does not exist in the system.

#include <fstream>

#include<iostream>

using namespace std;
int main() {
   FILE *codefile;
   if(codefile=fopen("abc.cpp","r")) {
cout<<"file exists";
fclose(codefile);
   } else {
cout<<"file doesn't exist";
   }
}

It shows the message in output.

Use isopen():

The file descriptor interface gives us a precise role for moving blocks of characters, but the stream interface also provides strong organized input as well as output functions as well as functions for the character- and line-oriented input and output. The relationship of a stream is engaged by its internal stream buffer, the function that is called isopen ().

#include <fstream>

#include<iostream>

using namespace std;

int main() {
fstreamfile;
file.open("Untitled1.cpp",ios :: in | ios :: trunc);
   if(file.is_open()) {
cout<<"file doesn't exists";
file.close();
   } else {
cout<<"file exist";
   }
}

First of all, we use two header files. After that, we go to the main body where we declare a variable for loading the file with the name of the file. We use the is_open function to open the file and pass the parameters with the name of the file and extension. In the end, we check with the if-else statement whether the file exists or not.

Conclusion:

In this article, we defined the file and also the significance of the file in our daily routine life. We also check if the file is present or not by using the C++ language. For this purpose, we use different types of liberties as well as functions to get files and also elaborate all the functions that we use to check the file by using a few lines of code. After these codes that we used for checking the file in the system with the help of C++, we came to know that there are multiple ways to check files that exist or not. The main thing we must keep in mind is the path of the file in the C++ code.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.