本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++ – 判断本机文件是否存在的方式总结
原文链接:https://www.stubbornhuang.com/1692/
发布于:2021年09月15日 14:23:55
修改于:2021年09月15日 14:23:55

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;
}
当前分类随机文章推荐
- C++ - Windows获取电脑上摄像头设备数目、名字以及id 阅读287次,点赞0次
- C++ - 常用的C++命令行参数解析第三方库 阅读2320次,点赞2次
- C++ - 智能指针的正确使用方式 阅读469次,点赞0次
- C++11/std::atomic - 原子变量(不加锁实现线程互斥) 阅读6119次,点赞2次
- C++ - 最简单的将文本文件的内容一次性读取到std::string的方法 阅读4541次,点赞4次
- C++ - Windows/Linux跨平台gbk与utf8字符集编码转换 阅读94次,点赞0次
- C++ - 将Unicode std::wstring字符串转换为Unicode std::string转义字符,类似于\uxxxx的形式 阅读1421次,点赞0次
- C++ - 导出接口函数和导出C++类 阅读195次,点赞0次
- C++11 - std::function简要介绍以及可包装函数的几种形式总结 阅读3049次,点赞0次
- C++ 11 - final关键字简要介绍 阅读1879次,点赞0次
全站随机文章推荐
- 资源分享 - Game Programming Gems 8 英文高清PDF下载 阅读2735次,点赞0次
- 资源分享 - An Introduction to Computational Fluid Dynamics - The Finite Volume Method (First Edition)英文高清PDF下载 阅读180次,点赞0次
- Pytorch - torch.distributed.init_process_group函数详解 阅读470次,点赞0次
- 计算机图形学 - 常用的3D数学知识备忘,如三角函数、向量运算、矩阵运算、图形学常用的平移缩放旋转矩阵,视图矩阵,投影矩阵 阅读1161次,点赞3次
- 资源分享 - Real-Time Rendering, Fourth Edition 英文高清PDF下载 阅读54045次,点赞21次
- 资源分享 - Digital Character Development - Theory and Practice , First Edition 英文高清PDF下载 阅读1308次,点赞0次
- Marching Cube(C++ OpenGl代码)读取医学三维图像*.raw进行三维重建 阅读6914次,点赞4次
- WordPress - 在编辑文章发布时弹出是否发布提醒框 阅读3203次,点赞0次
- WordPress - 后台登录成功/失败发送邮件给网站管理员 阅读2870次,点赞0次
- C++ - 使用宏区分不同系统平台、不同编译器、不同编译模式等编译期宏使用总结 阅读1203次,点赞0次
评论
167