HomeLinuxQuickest Strategy to Verify if a File Exists Utilizing Commonplace C++

Quickest Strategy to Verify if a File Exists Utilizing Commonplace C++


Checking if information exist utilizing customary C++ is a crucial activity for builders engaged on purposes associated to information and directories. Earlier than transferring any knowledge to the disk or trying to overwrite an present file, verifying whether or not the file exists is significant. Relying on what the developer is making an attempt to realize, there are a number of how to examine if a file exists. This text will train you additional about it.

The best way to Verify File Existence in Commonplace C++

There are quite a few strategies to search out out if a file is there, akin to the next:

1: The best way to Verify File Existence in C++ Utilizing stat() operate

The stat() technique is probably the most environment friendly and safe approach to confirm file existence. The sys/stat.h header file has a predefined operate known as stat. The operate accepts a path and a construction as parameters, the place the file or listing related metadata, if any, could be saved. The operate returns the results of 0 if the route is a legit one. We’d examine for the existence of the file for example:

#embody <iostream>

#embody <sys/stat.h>

utilizing namespace std;

int important()

{

    const char* FILE = “C_File.txt”;

    struct stat sb;

    if (stat(FILE, &sb) == 0)

       cout << “This file exists”;

    else

      cout << “The file doesn’t exists!”;

    return 0;

}

First, the FILE pointer variable shops the file’s path. After that, the stat header file’s format is used to initialize the empty construction. Metadata could be saved on this manner. The stat operate is then known as within the if situation. The outcome could be 0 if the trail is appropriate, that means the file or listing existed; in any other case, it could be non-zero.

Output

Notice: Guarantee changing the filename “C_File.txt” with the file title you need to examine.

2: The best way to Verify File Existence in C++ Utilizing std::ifstream() operate

Utilizing the C++ customary library operate std::ifstream is one other technique to find out whether or not a file is current. A stream object that could be used to learn the file is returned by this operate, which accepts a path as an enter. To make use of this operate, the developer would cross the trail and title of the file to the constructor of the std::ifstream class. The results of doing that is both the file being efficiently opened by the constructor, or an exception being thrown. Consequently, if the file is opened, the developer can assume that it’s current.

#embody <iostream>

#embody <fstream>

utilizing namespace std;

int important(){

ifstream file(“file_name.txt”);

    if (file.good())

    {

std::cout << “file exists.” << endl;

    }

    else

    {

std::cout << “file does not exist.” << endl;

    }

}

Beginning with the important() technique, an object of the ifstream class named file is created to learn the goal file later. When invoking the open operate on the file object, the vacation spot file title is then provided as an argument. This line will try and open the file in read-only mode.

Since a file might solely be opened if it bodily exists there and can’t be accessed in any other case. We’re not directly using the open() technique to confirm the file’s existence. Then, utilizing if-else conditional statements, we decide if the file object has opened the file or not; if it has, which means that it’s situated on the required path, and we show a hit message; in any other case, we produce an error message.

Output

3: The best way to Verify File Existence in C++ Utilizing fopen() operate

The third approach to examine if a file exists is to make use of the C++ operate fopen(). The fopen() technique creates a stream and opens the file indicated by filename. The mode variable is a personality string that signifies the sort of file entry that has been requested. One positional parameter precedes optionally available key phrase arguments within the mode variable.

We are able to save the return worth from the execution of fopen() within the pointer file when it has completed. If the file opening was profitable, the operate fopen(), which exhibits if the file had beforehand opened, will produce a file stream pointer referring to the goal file. If it was unsuccessful, which signifies if the file had already been, it can return NULL. Then, if the file reference isn’t NULL, we all know the file is current and will produce a hit message; in any other case, an error message will likely be despatched.

#embody <iostream>

#embody <fstream>

utilizing namespace std;

int important()

{

    FILE* file;

    file = fopen(“C_File.txt”, “r”);

    if (file!=NULL)

{

    cout << “File exists” << endl;

}

    else

{

    cout << “File doesn’t exists” << endl;

}

    return 0;

}

To learn the file, we construct a pointer to the FILE class ranging from the important () technique. Subsequent, we use the arguments “C_File.txt” and “r” to outline the goal file and the motion we need to carry out on it once we execute the fopen () technique. ‘r’ signifies that we need to learn the file.

Output

Conclusion

There are completely different C++ capabilities to examine if a file exists, that are stat, std::ifstream and fopen. Of the three strategies, stat() operate is the quickest and most dependable approach to examine file existences. Whereas different two capabilities are additionally helpful for checking the file existence. Subsequently, builders ought to think about using these capabilities for optimum efficiency and reliability when checking for file existence.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments