方法一:C++中比较简单的一种办法(使用文件流打开文件)
#include
#include
using namespace std;
#define FILENAME "*.dat" // 指定文件名
int main( void )
{
fstream _file;
_file.open(FILENAME, ios::in);
if(!_file)
{
cout<
}
else
{
cout<
}
cin.get();
return ;
}
方法二:利用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:
EACCESAccess 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, _accessdetermines 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
#include
#include
void main( void )
{
/* Check for existence */
if( (_access( "ACCESS.C", )) != - )
{
printf( "File ACCESS.C exists " );
/* Check for write permission */
if( (_access( "ACCESS.C", )) != - )
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[]);
hFind = FindFirstFile(argv[], &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 ;
}
(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
#include
#include
using namespace boost::filesystem;
int GetFilePath(std::string &strFilePath)
{
string strPath;
int nRes = ;
//指定路径
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 -;
}
}
strFilePath = full_path.native_directory_string();
return ;
}
python中OS模块操作文件和目录
在python中执行和操作目录和文件的操作是通过内置的python OS模块封装的函数实现的. 首先导入模块,并查看操作系统的类型: >>> import os os.name # ...
Linux中一个快速查找文件和目录的命令
功能介绍: locate命令其实是find -name的另一种写法,但是要比后者快得多,原因在于它不搜索具体目录,而是搜索一个数据库/var/lib/locatedb,值得注意的是:版本不同,会有所不 ...
Linux中常用命令(文件与目录)
1.pwd 查看当前目录(Print Working Directory) 2.cd 切换工作目录(Change Directory) (1)格式:cd [目录位置] 特殊目录: .当前目录 ..上一 ...
更改Anaconda中Jupyter的默认文件保存目录
转载:https://blog.csdn.net/u014552678/article/details/62046638 总结:修改Anaconda中的Jupyter Notebook默认工作路径的三 ...
【转】在cmd/bat脚本中获取当前脚本文件所在目录
一.关于cd的/d参数 关于cd 的/d参数,在cmd中敲入cd /?可以看到/d参数的解释如下: 使用 /D 命令行开关,除了改变驱动器的当前目录之外,还可改变当前驱动器.这句话不太好理解,我做个试 ...
如何在git中删除指定的文件和目录
部分场景中,我们会希望删除远程仓库(比如GitHub)的目录或文件. 具体操作 拉取远程的Repo到本地(如果已经在本地,可以略过) $ git clone xxxxxx 在本地仓库删除文件 $ gi ...
linux中的权限对于文件和目录的重要性
对于文件 r 可以读取文件的实际内容 w 可以编辑文件的内容 x 文件可以被系统执行 对于目录 r 具有读取目录的结构列表,也就是说你可以用ls命令查看目录下的内容列表 w 可以建立新的文件,删除文件 ...
攻城狮在路上(叁)Linux(十五)--- 文件与目录的默认权限与隐藏权限
一.文件默认权限:umask <==需要被减去的权限. 1.umask指的是当前用户在新建文件或者目录时的默认权限,如0022; 2.默认情况下,用户创建文件的最大权限为666; 创建目录的最大 ...
Python之文件与目录操作及压缩模块(os、shutil、zipfile、tarfile)
Python中可以用于对文件和目录进行操作的内置模块包括: 模块/函数名称 功能描述 open()函数 文件读取或写入 os.path模块 文件路径操作 os模块 文件和目录简单操作 zipfile模 ...
随机推荐
React Native学习笔记之2
1:如何创建一个react native工程 首先进入到指定文件夹里面,然后在终端执行react-native init ReactNativeProject :其中ReactNativeProjec ...
nodejs中Stream的理解
在nodejs中可以通过fs模块读写文件,我们来看下fs模块提供的接口: fs.readFile(filename, callback) 异步读取文件. filename是读取文件的文件名,如果是相对 ...
深入理解计算机系统(2.7)---二进制浮点数,IEEE标准(重要)
2.6我们进行了二进制整数运算的最后一役,本次LZ将和各位一起进入浮点数的世界,这里没有无符号,没有补码,但是有各种各样的惊奇.倘若你真正的进入了浮点数的世界,一定会发现它原来是这么有意思,而不是像之 ...
DNS(二)之构建域名解析缓存
域名解析缓存的必要性 在部署服务的时候,很多程序需要使用域名解析的功能,一般配置/etc/resovl.conf去指定DNS服务器的IP,但是如果程序发起的请求量较大,那么服务器就容易被DNS服务器禁 ...
ls 只显示目录
只显示目录: ls -d */ 在实际应用中,我们有时需要仅列出目录,下面是 4 种不同的方法. 1. 利用 ls 命令的 -d 选项: $ ls -d */ 2. 利用 ls 命令的 -F 选项: ...
SQL Case When Then
适用sqlServer,oracle CASE WHEN 条件1 THEN 结果1 WHEN 条件2 THEN 结果2 WHEN 条件3 THEN 结果3 WHEN 条件4 T ...
php的运行环境介绍
php软件已下载在我的百度云:页面底部有地址,如有需要欢迎下载! 一:如何让php环境运行php代码? 直接使用php软件直接运行代码文件中的php代码 在B/S结构中让Apache使用php软件运行 ...
spring事物传播机制 事物隔离级别
Spring事务类型详解: PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务.这是最常见的选择. PROPAGATION_SUPPORTS--支持当前事务,如 ...
Mysql 5.6主从同步配置与解决方案
主库IP:192.168.1.10 从库IP:192.168.1.11 centos的mysql配置文件在:/etc/my.cnf 1.主库配置编辑my.cnf: # 启用二进制日志 log_bin ...
SpringBoot启动原理及相关流程
一.springboot启动原理及相关流程概览 springboot是基于spring的新型的轻量级框架,最厉害的地方当属自动配置.那我们就可以根据启动流程和相关原理来看看,如何实现传奇的自动配置 二 ...