方法一: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 ;
}

方法二:利用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", )) != - )
{
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 <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 = ;
//指定路径
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 ;
}

C/C++ 中判断某一文件或目录是否存在的更多相关文章

  1. python中OS模块操作文件和目录

    在python中执行和操作目录和文件的操作是通过内置的python OS模块封装的函数实现的. 首先导入模块,并查看操作系统的类型: >>> import os os.name # ...

  2. Linux中一个快速查找文件和目录的命令

    功能介绍: locate命令其实是find -name的另一种写法,但是要比后者快得多,原因在于它不搜索具体目录,而是搜索一个数据库/var/lib/locatedb,值得注意的是:版本不同,会有所不 ...

  3. Linux中常用命令(文件与目录)

    1.pwd 查看当前目录(Print Working Directory) 2.cd 切换工作目录(Change Directory) (1)格式:cd [目录位置] 特殊目录: .当前目录 ..上一 ...

  4. 更改Anaconda中Jupyter的默认文件保存目录

    转载:https://blog.csdn.net/u014552678/article/details/62046638 总结:修改Anaconda中的Jupyter Notebook默认工作路径的三 ...

  5. 【转】在cmd/bat脚本中获取当前脚本文件所在目录

    一.关于cd的/d参数 关于cd 的/d参数,在cmd中敲入cd /?可以看到/d参数的解释如下: 使用 /D 命令行开关,除了改变驱动器的当前目录之外,还可改变当前驱动器.这句话不太好理解,我做个试 ...

  6. 如何在git中删除指定的文件和目录

    部分场景中,我们会希望删除远程仓库(比如GitHub)的目录或文件. 具体操作 拉取远程的Repo到本地(如果已经在本地,可以略过) $ git clone xxxxxx 在本地仓库删除文件 $ gi ...

  7. linux中的权限对于文件和目录的重要性

    对于文件 r 可以读取文件的实际内容 w 可以编辑文件的内容 x 文件可以被系统执行 对于目录 r 具有读取目录的结构列表,也就是说你可以用ls命令查看目录下的内容列表 w 可以建立新的文件,删除文件 ...

  8. Linux中权限对于文件和目录的区别

    Linux系统中的权限对于文件和目录来说,是有一定区别的 下面先列举下普通文件对应的权限 1)可读r:表示具有读取.浏览文件内容的权限,例如,可以对文件执行 cat.more.less.head.ta ...

  9. 攻城狮在路上(叁)Linux(十五)--- 文件与目录的默认权限与隐藏权限

    一.文件默认权限:umask <==需要被减去的权限. 1.umask指的是当前用户在新建文件或者目录时的默认权限,如0022; 2.默认情况下,用户创建文件的最大权限为666; 创建目录的最大 ...

随机推荐

  1. Java 基础【13】 文件(文件夹) 创建和删除

    使用 java.io.file 创建文件(文件夹),算是 java 最基础的知识,但实战项目中还是需要知晓细节. 比如 File 类中的 mkdir() 和 mkdirs() 的区别. JDK API ...

  2. RecyclerView的使用(四)

    前面我们已经实现了RecyclerView的大部分功能了,但是有个很明显的缺陷-------没有点击效果!这就坑爹了 ListView自带点击效果好嘛!连这个都要自己定义.... 话不多说,下面就来为 ...

  3. CSS编写技巧

    1.尽量少的使用全局的重置代码 全局重置代码:*{margin:0; padding:0;}对于熟悉CSS的人来说并不陌生,并且有很多人的第一句CSS代码就是这句.它可以避免不同浏览器的默认间距不同而 ...

  4. 分布式缓存Redis使用心得

    一.缓存在系统中用来做什么 1. 少量数据存储,高速读写访问.通过数据全部in-momery 的方式来保证高速访问,同时提供数据落地的功能,实际这正是Redis最主要的适用场景. 2. 海量数据存储, ...

  5. ajax 多个表单值问题,表单序列化加其它表单值

    $.ajax({ type: "post", url: "{:u('cart/totalByCard')}?t="+Math.random(9999), dat ...

  6. nginx文件管理

    管理文件下载nginx 可以自己实现,无需写代码即可: 修改配置文件: location /doc { autoindex on; autoindex_exact_size on; autoindex ...

  7. MapReduce实现手机上网日志分析(分区)

    一.问题背景 实际业务的需要,比如以移动为例,河南的用户去了北京上网,那么他的上网信息默认保存在了北京的基站,那么我们想要查询北京地区的上网日志信息默认也包含了其他地区用户的在本区的上网信息,否则只能 ...

  8. Linux 内核数据结构:双向链表

    Linux 内核提供一套双向链表的实现,你可以在 include/linux/list.h 中找到.我们以双向链表着手开始介绍 Linux 内核中的数据结构 ,因为这个是在 Linux 内核中使用最为 ...

  9. Python Day6

    面向对象 概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发"更快更好更强...&qu ...

  10. eclipse中导入jar文件的源码

    有时候想看看一个jar包的源码是怎么写的,想按Ctrl+鼠标左键点击来自动导航这时候就需要先把源码给导入到eclipse中,步骤如下:首先准备jar包和源文件包比如: