2012年12月21日星期五

C++ Check File Exists

Check whether a file exists:


#include <sys/stat.h>
// Function: fileExists
/**
    Check if a file exists
@param[in] filename - the name of the file to check

@return    true if the file exists, else false

*/
bool fileExists(const std::string& filename)
{
    struct stat buf;
    if (stat(filename.c_str(), &buf) != -1)
    {
        return true;
    }
    return false;
}

Not sure whether it works on all platforms.

Source:StackOverFlow

沒有留言:

發佈留言