Start New Discussion within our Software Development Community

Hi,

I wanna ask you if there is SIMPLE way to check if file called XXX is in path YYY? I have this Code:

int GetLocation()
{
	GetName();
	string location;
	location += "C:\\Users\\" + name + "\\Documents\\test.txt";
	if(location)
		return 1;
	else
		return 0;
}

int main()
{
	if(GetLocation() == 1)
		Start();
	else
		ExitProcess(0);
	return 0;
}

You can try opening it for reading and check if it's open. If it's not open, it doesn't exist.

//...

#include <fstream>
#include <string>

//...

std::string filename;

//...

std::ifstream fin(filename.c_str());

if (fin.is_open()) { /* file exists */ }
else { /* file does not exist */ }

//...

Useful link -> http://cplusplus.com/doc/tutorial/files/

thanks,

I have one more question, does this works with other files? like .exe etc?

well, I'm totally confused when I read something at msdn, I'm just beginner and I really have no idea what that codes there means, so I don't wanna use them, and that thing with fstream works fine, so thanks! :)

This article has been dead for over six months. Start a new discussion instead.