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

C++File

C++ Problem Overview


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.

C++ Solutions


Solution 1 - C++

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

Solution 2 - C++

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 
	}
}

Solution 3 - C++

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.

Solution 4 - C++

A simple way from http://www.cplusplus.com/forum/general/1796/

ifstream ifile(filename);
if (ifile) {
  // The file exists, and is open for input
}

Solution 5 - C++

You can use stat, which should be portable across platforms and is in the standard C library:

#include <sys/stat.h>

bool FileExists(string filename) {
    struct stat fileInfo;
    return stat(filename.c_str(), &fileInfo) == 0;
}

If stat returns 0, the file (or directory) exists, otherwise it doesn't. I assume that you'll have to have access permissions on all directories in the file's path. I haven't tested portability, but this page suggests it shouldn't be an issue.

Solution 6 - C++

A better way:

std::ifstream stream;
stream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
stream.open(fileName, std::ios::binary);

Solution 7 - C++

Let's me give example with real running:

  1. file does't exist:

enter image description here

  1. file exist:

enter image description here

see http://www.cplusplus.com/reference/fstream/ifstream/ for more information about its public function.

Solution 8 - C++

Straight way without creating ifstream object.

if (!std::ifstream(filename))
{
	 // error! file doesn't exist.
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaul NathanView Question on Stackoverflow
Solution 1 - C++SwarthyMantoothView Answer on Stackoverflow
Solution 2 - C++Khaled AlshayaView Answer on Stackoverflow
Solution 3 - C++CogwheelView Answer on Stackoverflow
Solution 4 - C++LI XuhongView Answer on Stackoverflow
Solution 5 - C++Erik GarrisonView Answer on Stackoverflow
Solution 6 - C++user1633272View Answer on Stackoverflow
Solution 7 - C++JayhelloView Answer on Stackoverflow
Solution 8 - C++ArunView Answer on Stackoverflow