C/C++判断文件是否存在

The following is the most common method for checking if file exist:

#include <sys/stat.h>

bool FileExist(const char* FileName)
{
    struct stat my_stat;
    return (stat(FileName, &my_stat) == 0);
}

//Example Usage
int main(int argc, char* argv[])
{
    bool v1 = FileExist("c:\\autoexec.bat");
    bool v2 = FileExist("c:\\nofile.bat");
    bool v3 = FileExist("c:\\config.sys");
    bool v4 = FileExist("c:\\nofile2.bat");
    return 0;
}

This method works on ALL platforms I have used (Windows, Unix, Linux).

And it does not require opening the file, which could cause problems on some platforms if the file has protected property settings.

This method works both in C and in C++.

Although stat is not part of the standard, it is supported by all major platforms and compilers.

原文链接: https://www.cnblogs.com/liuyunfeifei/archive/2013/03/26/2982937.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/82064

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午8:25
下一篇 2023年2月9日 下午8:25

相关推荐