Tuesday 25 October 2016

c++ - How to check if a file exists (regardless of its file type)?

Edit: The problem wasn't with my c++. The problem was with how I was calling the shared library in GameMaker Studio, which was why it wasn't working.



I'm trying to detect whether a file exists, and I've tried many methods over the internet and none of them seem to work. Here's an example:



double file_exists(char *filename)
{
std::ifstream infile(filename);

return infile.good();
}


I assume it will only work with text files. for example, this won't work:



file_exists("/usr/bin/gedit");


...because that is a Linux executable.




I want a means to check whether a file exists regardless of its file type.



On Windows, this can easily be achieved like so:



double file_exists(char* filename)
{
DWORD fileAtt = GetFileAttributesA(filename);
if(fileAtt == INVALID_FILE_ATTRIBUTES)
throw GetLastError();


return (bool)((fileAtt&FILE_ATTRIBUTE_DIRECTORY) == 0);
}


But I'm on Linux and I need an equivalent of the above snippet that will work on Linux.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...