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
当前分类随机文章推荐
- C++ - std::map - 存储动态指针时正确释放内存 阅读4645次,点赞1次
- C++ - std::unordered_map中使用结构体或者vector等复杂的数据结构作为Key值 阅读1337次,点赞0次
- C++ - std::string字符串格式化方法总结 阅读2023次,点赞0次
- C++11 - 使用std::chrono计算程序、函数运行时间 阅读3079次,点赞0次
- C++ - 想用C++写服务器?C/C++ web服务第三方库、框架整理 阅读634次,点赞0次
- C++ - 求解std::vector
中topk数值以及topk数值对应的索引 阅读3321次,点赞0次 - C++ - Windows下字符串UTF8编码转ANSI,ANSI转UTF8编码 阅读803次,点赞0次
- C++11 - 构建一个符合实际应用要求的线程池 阅读1437次,点赞0次
- CMake - 链接多线程pthread库的几种方式 阅读78次,点赞0次
- C++ - Windows和Linux系统下获取当前可执行程序的绝对路径 阅读3077次,点赞0次
全站随机文章推荐
- 三维重建 - 基于RBF的三维网格重建 阅读5952次,点赞7次
- 资源分享 - 深入应用C++ 11代码优化与工程级应用(祁宇著)PDF下载 阅读6904次,点赞1次
- 深度学习 - 深度学习中的术语/专有名词归纳 阅读1010次,点赞0次
- TensorRT - Using an engine plan file across different models of devices is not recommended and is likely to affect performance or even cause errors 阅读596次,点赞0次
- 资源分享 - Vector Analysis for Computer Graphics , Second Edition 英文高清PDF下载 阅读1373次,点赞0次
- C++ - 基于no-boost Asio实现一个异步TCP服务器 阅读147次,点赞0次
- Windows Terminal、Power Shell设置conda环境,修复不显示conda虚拟环境名称的问题 阅读556次,点赞0次
- 资源分享 - Cloth Modeling and Animation 英文高清PDF下载 阅读1513次,点赞0次
- 资源分享 - Character Animation With Direct3D 英文高清PDF下载 阅读2271次,点赞1次
- Pytorch - 用Pytorch实现ResNet 阅读1025次,点赞0次
评论
169