Monday, 2 October 2017

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

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...