Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ifstream: how to tell if specified file doesn't exist

Tags:

c++

file

I want to open a file for reading. However, in the context of this program, it's OK if the file doesn't exist, I just move on. I want to be able to identify when the error is "file not found" and when the error is otherwise. Otherwise means I need to quit and error.

I don't see an obvious way to do this with fstream.


I can do this with C's open() and perror(). I presumed that there was a fstream way to do this as well.

like image 237
Paul Nathan Avatar asked Oct 08 '22 08:10

Paul Nathan


People also ask

How do I know if an ifstream file exists?

A simple function: bool exist(string PATH) { ifstream fin; fin. open(PATH. c_str()); return bool(fin); } Now, checking : if(exist("a. txt")) { //fstream file; //file.

How do I check if a file exists in C++?

Use ifile. open(): ifile. open() is mainly used to check if a file exists in the specific directory or not.

How check if file is empty C++?

The above code works in a simple manner: peek() will peek at the stream and return, without removing, the next character. If it reaches the end of file, it returns eof() . Ergo, we just peek() at the stream and see if it's eof() , since an empty file has nothing to peek at.

Does ifstream automatically close the file?

Note that any open file is automatically closed when the ifstream object is destroyed.


3 Answers

EDIT: I've been notified that this does not necessarily indicate a file does not exist, as it may be flagged due to access permissions or other issues as well.

I know I'm extremely late in answering this, but I figured I'd leave a comment anyway for anyone browsing. You can use ifstream's fail indicator to tell if a file exists.

ifstream myFile("filename.txt");
    if(myFile.fail()){
        //File does not exist code here
    }
//otherwise, file exists
like image 94
SwarthyMantooth Avatar answered Oct 19 '22 08:10

SwarthyMantooth


I don't think you can know if "the file doesn't exist". You could use is_open() for generic checking:

ofstream file(....);
if(!file.is_open())
{
  // error! maybe the file doesn't exist.
}

If you are using boost you could use boost::filesystem:

#include <boost/filesystem.hpp>
int main()
{
    boost::filesystem::path myfile("test.dat");

    if( !boost::filesystem::exists(myfile) )
    {
        // what do you want to do if the file doesn't exist 
    }
}
like image 46
Khaled Alshaya Avatar answered Oct 19 '22 07:10

Khaled Alshaya


Since the result of opening a file is OS-specific, I don't think standard C++ has any way to differentiate the various types of errors. The file either opens or it doesn't.

You can try opening the file for reading, and if it doesn't open (ifstream::is_open() returns false), you know it either doesn't exist or some other error happened. Then again, if you try to open it for writing afterwards and it fails, that might fall under the "something else" category.

like image 24
Cogwheel Avatar answered Oct 19 '22 06:10

Cogwheel