• 在本站开通年度VIP,无限制下载本站资源和阅读本站文章

  • 问题反馈可发送邮件到stubbornhuang@qq.com

  • 本站由于前段时间遭受到大量临时和国外邮箱注册,所以对可注册的邮箱类型进行了限制!

  • 欢迎大家交换友链,可在https://www.stubbornhuang.com/申请友情链接进行友链交换申请!

  • 如果觉得本站的内容有帮助,可以考虑打赏博主哦!

  • 计算机图形学与计算几何经典必备书单整理,下载链接可参考:https://www.stubbornhuang.com/1256/

  • 感谢大家访问本站,希望本站的内容可以帮助到大家!

  • 工资「喂饱肚子」,副业「养活灵魂」!

  • 本站会放置Google广告用于维持域名以及网站服务器费用。

C++ – 判断本机文件是否存在的方式总结

C++ 发布于2021-09-15 阅读 12,111次 0次评论 0次点赞 本文共2485个字,阅读需要7分钟。

C++ - 判断本机文件是否存在的方式总结

由于C++没有像python那样方便的os.path官方库,经常面临着判断一个文件是否在本机上存在都不易用,今天就总结下C++下判断本机文件是否存在的一些方法。

1 通用方法

1.1 使用std::ifstream判断

#include <iostream>
#include <fstream>

bool Is_File_Exist(const std::string& file_path)
{
    std::ifstream file(file_path.c_str());
    return file.good();
}

int main()
{
    if (Is_File_Exist("test.txt"))
    {
        std::cout << "文件存在" << std::endl;
    }
    else
    {
        std::cout << "文件不存在" << std::endl;
    }

    return 0;
}

1.2 使用fopen判断

#include <iostream>
#include <fstream>

bool Is_File_Exist(const std::string& file_path)
{
    if (FILE* file = fopen(file_path.c_str(), "r"))
    {
        fclose(file);
        return true;
    }
    else 
    {
        return false;
    }
}

int main()
{
    if (Is_File_Exist("test.txt"))
    {
        std::cout << "文件存在" << std::endl;
    }
    else
    {
        std::cout << "文件不存在" << std::endl;
    }

    getchar();

    return 0;
}

1.3 使用C++ 17的std::filesystem::exists判断

如使用vs2019并强制设置C++语言标准为C++17,linux请选择合适的GCC或者G++版本

#include <iostream>
#include <filesystem>


bool Is_File_Exist(const std::string& file_path)
{
    return std::filesystem::exists(file_path);
}

int main()
{
    if (Is_File_Exist("test.txt"))
    {
        std::cout << "文件存在" << std::endl;
    }
    else
    {
        std::cout << "文件不存在" << std::endl;
    }

    getchar();

    return 0;
}

1.4 使用access判断

#include <iostream>

#ifdef _WIN32
#include <io.h> 
#define access    _access_s
#else
#include <unistd.h>
#endif

bool Is_File_Exist(const std::string& file_path)
{
    return access(file_path.c_str(), 0) == 0;
}

int main()
{
    if (Is_File_Exist("test.txt"))
    {
        std::cout << "文件存在" << std::endl;
    }
    else
    {
        std::cout << "文件不存在" << std::endl;
    }

    getchar();

    return 0;
}

1.5 使用stat判断

#include <iostream>
#include <Windows.h>

bool Is_File_Exist(const std::string& file_path)
{
    struct stat buffer;
    return (stat(file_path.c_str(), &buffer) == 0);
}

int main()
{
    if (Is_File_Exist("test.txt"))
    {
        std::cout << "文件存在" << std::endl;
    }
    else
    {
        std::cout << "文件不存在" << std::endl;
    }

    getchar();

    return 0;
}

2 Windows上的方法

2.1 使用Windows API CreateFile判断

#include <iostream>
#include <Windows.h>

bool Is_File_Exist(const std::wstring& file_path)
{
    bool result = true;

    HANDLE fileHandle = CreateFile(
        file_path.c_str(),
        GENERIC_READ,
        0,
        NULL,
        OPEN_EXISTING,
        0,
        NULL);

    if ((fileHandle != NULL) && (fileHandle != INVALID_HANDLE_VALUE))
        CloseHandle(fileHandle);
    else
    {
        DWORD error = GetLastError();

        if ((error == ERROR_FILE_NOT_FOUND) || (error == ERROR_PATH_NOT_FOUND))
            result = false;
    }

    return result;
}

int main()
{
    if (Is_File_Exist(L"test.txt"))
    {
        std::cout << "文件存在" << std::endl;
    }
    else
    {
        std::cout << "文件不存在" << std::endl;
    }

    getchar();

    return 0;
}

2.2 使用Windows API GetFileAttributes判断

#include <iostream>
#include <Windows.h>

bool Is_File_Exist(const std::wstring& file_path)
{
    return GetFileAttributes(file_path.c_str()) != INVALID_FILE_ATTRIBUTES;
}

int main()
{
    if (Is_File_Exist(L"test.txt"))
    {
        std::cout << "文件存在" << std::endl;
    }
    else
    {
        std::cout << "文件不存在" << std::endl;
    }

    getchar();

    return 0;
}

欢迎扫码关注我的微信公众号,及时获取文章更新

微信公众号二维码

本文作者:StubbornHuang

版权声明:本文为站长原创文章,如果转载请注明原文链接!

原文标题:C++ – 判断本机文件是否存在的方式总结

原文链接:https://www.stubbornhuang.com/1692/

发布于:2021年09月15日 14:23:55

修改于:2023年06月26日 21:16:42

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

文章末尾
上一篇
资源分享 - Graphics Programming Methods 英文PDF下载
计算几何与计算机图形学资源
下一篇
资源分享 - Principles of Digital Image Synthesis , Volume 1 and Volume 2 英文PDF下载
计算几何与计算机图形学资源
当前分类随机文章推荐

发表评论

您必须 [ 登录 ] 才能发表留言!

关注我们的公众号

微信公众号