C++中判断某一文件或目录是否存在
C++中判断某一文件或目录是否存在
方法1. C++中较简单方法(使用文件流打开文件)
#include <iostream>
#include <fstream>
using namespace std;
#define FILENAME "*.dat" // 指定文件名
int main( void )
{
fstream _file;
_file.open(FILENAME, ios::in);
if(!_file)
{
cout<<FILENAME<<"没有被创建!"<<endl;
}
else
{
cout<<FILENAME<<"已经存在!"<<endl;
}
cin.get();
return 0;
}
方法2. 利用C语言库函数(_access)
函数原型:
int _access( const char *path, int mode )
函数参数:
path:文件路径。
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
方法3. 使用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;
}
方法4. 使用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;
}
C++中判断某一文件或目录是否存在相关推荐
- C/C++ 中判断某一文件或目录是否存在
方法一:C++中比较简单的一种办法(使用文件流打开文件) 1 #include <iostream> 2 #include <fstream> 3 4 using namesp ...
- 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的工作方式是使 ...
最新文章
- React Hooks-概览
- matlab实现将一个文件夹里面的pcm文件依次合并成一个信号的函数
- Linux更改终端的用户名和主机名的颜色
- c#使用正则表达式获取TR中的多个TD_PHP正则表达式技术心得与使用技巧完全详解 第3节...
- java随机数排序算法_理解快速排序算法
- hadoop--集群时间同步(可不同步)
- 收到计算机系统公司退款会计分录,企业账户收到退款,怎么做账务处理?
- High ASCII字符从bat文件到dos控制台的转化问题
- POJ 2068 NIm (dp博弈,每个人都有特定的取最大值)
- Linux中的用户切换:su和su - 的区别
- DOM 元素对象解析
- 求字符串转化的最小操作次数 DP动态规划
- cim系统(cim系统包含哪些部分)
- 速途在线沙龙11期:王通夫唯首次聚首共话SEO
- IDEA出现error moudle not specified
- markman,让设计更有爱!
- MySQL无法连接/端口被占用[解决记录]
- 25. Green Living 绿色生活
- svc的参考文献_浅谈SVC的原理及作用
- Latex overleaf 图表公式参考文献