C/C++ 中判断某一文件或目录是否存在
方法一:C++中比较简单的一种办法(使用文件流打开文件)
1 #include <iostream> 2 #include <fstream> 3 4 using namespace std; 5 6 #define FILENAME "*.dat" // 指定文件名 7 8 int main( void ) 9 { 10 fstream _file; 11 _file.open(FILENAME, ios::in); 12 if(!_file) 13 { 14 cout<<FILENAME<<"没有被创建!"<<endl; 15 } 16 else 17 { 18 cout<<FILENAME<<"已经存在!"<<endl; 19 } 20 21 cin.get(); 22 return 0; 23 }
方法二:利用C语言库函数(_access)
函数原型:
int _access( const char *path, int mode )
函数参数:
l path:文件路径
l mode:读写属性
返回值(MSDN):
Each of these functions returns 0 if the file has the given mode. The function returns –1 if the named file does not exist or is not accessible in the given mode; in this case, errno is set as follows:
EACCES Access denied: file’s permission setting does not allow specified access.
ENOENT Filename or path not found.
EINVAL Invalid parameter.
函数功能(MSDN):
When used with files, the _access function determines whether the specified file exists and can be accessed as specified by the value of mode(见下图表). When used with directories, _access determines only whether the specified directory exists; in Windows NT, all directories have read and write access.
/* ACCESS.C: This example uses _access to check the* file named "ACCESS.C" to see if it exists and if* writing is allowed.*/#include <io.h> #include <stdio.h> #include <stdlib.h>void main( void ) {/* Check for existence */if( (_access( "ACCESS.C", 0 )) != -1 ){printf( "File ACCESS.C exists " );/* Check for write permission */if( (_access( "ACCESS.C", 2 )) != -1 )printf( "File ACCESS.C has write permission " );} }输出: >>File ACCESS.C exists. >>File ACCESS.C has write permission
方法三:使用Windows API函数FindFirstFile(...)
(1) 检查某一文件是否存在:
#include "windows.h" int main(int argc, char *argv[]) {WIN32_FIND_DATA FindFileData;HANDLE hFind;printf ("Target file is %s. ", argv[1]);hFind = FindFirstFile(argv[1], &FindFileData);if (hFind == INVALID_HANDLE_VALUE){printf ("Invalid File Handle. Get Last Error reports %d ", GetLastError ());}else{printf ("The first file found is %s ", FindFileData.cFileName);FindClose(hFind);}return 0; }
(2) 检查某一目录是否存在:
// 目录是否存在的检查: BOOL CheckFolderExist(const string &strPath) {WIN32_FIND_DATA FindFileData;BOOL bValue = false;HANDLE hFind = FindFirstFile(strPath.c_str(), &FindFileData);if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)){bValue = TRUE; }FindClose(hFind);return bValue; }
方法四:使用boost库中filesystem类库的exists函数
#include <boost/filesystem/operations.hpp> #include <boost/filesystem/path.hpp> #include <boost/filesystem/convenience.hpp>using namespace boost::filesystem;int GetFilePath(std::string &strFilePath) {string strPath;int nRes = 0;//指定路径 strPath = "C:\"; path full_path( initial_path() );full_path = system_complete( path(strPath, native ) );//判断各级子目录是否存在,不存在则需要创建if ( !exists( full_path ) ){bool bRet = create_directories(full_path);if (false == bRet){return -1;}}strFilePath = full_path.native_directory_string();return 0; }
转载于:https://www.cnblogs.com/MakeView660/p/6101251.html
C/C++ 中判断某一文件或目录是否存在相关推荐
- C++中判断某一文件或目录是否存在
C++中判断某一文件或目录是否存在 方法1. C++中较简单方法(使用文件流打开文件) #include <iostream> #include <fstream> using ...
- linux中如何压缩目录文件,在Linux中,如何压缩文件和目录
Zip文件是包含一个或多个压缩文件或目录的数据容器,与未压缩的文件相比,压缩文件占用的磁盘空间更少,可以更快地从一台计算机传输到另一台计算机.使用适用于所有操作系统的实用程序,可以在Windows,m ...
- Linux中常用命令(文件与目录)
1.pwd 查看当前目录(Print Working Directory) 2.cd 切换工作目录(Change Directory) (1)格式:cd [目录位置] 特殊目录: .当前目录 ..上一 ...
- Linux 删除权限 umask,linux中的umask控制文件或目录的默认权限
umask命令可以控制着创建文件或目录时指定给文件或目录的默认权限.它使用八进制表示法表示从文件模式属性中删除一个位掩码.使用不带任何参数的umask命令,查看当前掩码值.一般都是0002或0022. ...
- linux如何找大文件夹,Linux系统中如何查找大文件或目录文件夹的方法
Linux系统中如何查找大文件或文件夹的方法 在Windows系统中,我们可以使用TreeSize工具查找一些大文件或文件夹,非常的方便高效,在Linux系统中,如何去搜索一些比较大的文件呢?下面我整 ...
- linux中的find查找文件或者目录、locate快速定位文件路径
1.7 搜索查找类 1.7.1 find查找文件或者目录 find 指令将从指定目录向下递归地遍历其各个子目录,将满足条件的文件显示在终端 find [搜索范围] [选项] 选项 功能 -name&l ...
- Linux系统中如何查找大文件或目录文件夹的方法
Linux系统中如何查找大文件或文件夹的方法 在Windows系统中,我们可以使用TreeSize工具查找一些大文件或文件夹,非常的方便高效,在Linux系统中,如何去搜索一些比较大的文件呢?下面我整 ...
- linux aide使用方法,如何在Linux中使用“AIDE”检查文件和目录的完整性
在我们关于加强和保护CentOS 7的大型指南中,在" 内部保护系统 "一节中,我们列出的用于内部系统保护以防病毒,rootkit,恶意软件和检测未授权活动的有用安全工具之一是AI ...
- git中怎样忽略.idea/文件和目录
Git是一个流行的版本控制系统.它是开发人员如何在项目中协作和工作的方式. Git允许您跟踪随着时间推移对项目所做的更改.除此之外,如果您想撤消更改,它还允许您恢复到以前的版本. Git的工作方式是使 ...
最新文章
- 软件之最大全电子书免费版
- mysql分组函数按月份差,学习猿地-mysql如何按月份分组查询
- 模块加载过程代码分析1
- 3 x 10的python表达式_已知 x = [3, 5, 7],那么表达式 x[10:]的值为_学小易找答案
- 汇编:键盘中断(int 9)
- 框架:初识Mybatis
- [Spring5]IOC容器_底层原理
- 牛客网 【每日一题】5月12日题目精讲 模拟战役
- php getcount_PHP中关键字interface和implements详解
- 西部数码域名解析到阿里云_西部数码云主机好吗 稳定性如何
- 八分音符(频率)卷积算子 Octave Convolution
- 一个关于C++ Inline关键字的引发的一个错误
- C++编程语言中类对象的赋值与复制介绍(三)
- Android Binder机制简单了解
- 【重新挂载磁盘空间】Linux系统/home的磁盘空间重新挂载给/root
- 最新借条模板,如何写
- pyton构建一个计算列表中位数的函数
- EasyUI+JavaWeb奖助学金管理系统[18]-奖助学金申请功能开发
- Lua语言编程学习之路01----第五章 函数
- 私钥,公钥,密钥的理解,不要钻死胡同
热门文章
- 安卓关于图片压缩的那些事儿,希望给每个安卓开发人员一些帮助
- Android属性动画进阶用法
- 2016OSC源创会年终盛典-综合技术专场-张小刚
- linux实例 批量修改图片文件名
- Silverlight三维柱状图3D饼图的Silverlight图表组件案例
- 配置u-boot的ip、网卡ip的命令
- php爱奇艺筛选标签,三种排序 快速筛选好视频_软件资讯技巧应用-中关村在线
- 《操作系统》OS学习(五):连续内存分配 内存碎片、动态分配、碎片整理、伙伴系统
- Flask中的 url_for() 函数
- 机器学习之聚类算法的原理推导及相关知识总结