2006年12月28日星期四

使用c/c++判断文件是否存在

// Test whether a file is exist or not
bool fileExists(const char* fileName)
{
    if (!fileName) return false;


#ifdef WIN32
    struct _stat buf;
    int result;

    result = _stat(fileName, &buf);

    if (result) {
        return false;
    }

    return (buf.st_mode & _S_IFMT) == _S_IFREG;
#else
    struct stat buf;
    int result;

    result = stat(fileName, &buf);

    if (result) {
        return false;
    }

    return (buf.st_mode & S_IFMT) == S_IFREG;
#endif
}

没有评论: