c/c++ c++ file c++/stl/boost 判断文件目录是否存在

1.C++很简单的一种办法:
#include <iostream>
#include 
<fstream>
using namespace  std;
#define FILENAME "stat.dat"

int  main()
{
     fstream _file;
     _file.open(FILENAME,ios::
in
);
     
if(!
_file)
     {
         cout
<<FILENAME<<"没有被创建"
;
      }
      
else

      {
          cout
<<FILENAME<<"已经存在" ;
      }
      
return 0
;
}

 

2.利用 c 语言的库的办法:

函数名: access
功  能: 确定文件的访问权限
用  法: int access(const char *filename, int amode);
以前一直没用过这个函数,今天调试程序发现了这个函数,感觉挺好用,尤其是判断一个文件或文件夹是否存在的时候,用不着再find了,文件的话还可以检测读写权限,文件夹的话则只能判断是否存在,下面摘自MSDN:

int _access( const char *path, int mode );

Return Value

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.

Parameters

path

File or directory path

mode

Permission setting

Remarks

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.

mode Value            Checks File For
00                              Existence only
02                              Write permission
04                              Read permission
06                              Read and write permission

Example


   
   
/*  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 "
 );
   }
}
Output
File ACCESS.C existsFile ACCESS.C has write permission

 

3.在windows平台下用API函数FindFirstFile(...):

(1)检查文件是否存在:

#define _WIN32_WINNT 0x0400

#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  wfd;
    
bool rValue = false
;
    HANDLE hFind 
= FindFirstFile(strPath.c_str(), &
wfd);
    
if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes &
 FILE_ATTRIBUTE_DIRECTORY))
    {
        rValue 
= true
;   
    }
    FindClose(hFind);
    
return
 rValue;
}

4.使用boost的filesystem类库的exists函数

#include <boost/filesystem/operations.hpp>
#include 
<boost/filesystem/path.hpp>
#include 
<boost/filesystem/convenience.hpp>

int GetFilePath(std::string & strFilePath)
{
    
string
 strPath;
    
int nRes = 0
;

    
//指定路径            

    strPath = "D:myTest est1 est2" ;
    
namespace fs =
 boost::filesystem;

    
//路径的可移植

    fs::path full_path( fs::initial_path() );
    full_path 
=
 fs::system_complete( fs::path(strPath, fs::native ) );
    
//判断各级子目录是否存在,不存在则需要创建

    if ( ! fs::exists( full_path ) )
    {
        
// 创建多层子目录

        bool bRet =  fs::create_directories(full_path);
        
if (false ==
 bRet)
        {
            
return -1
;
        }

    }
    strFilePath 
=
 full_path.native_directory_string();

    
return 0
;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
这上传的资源中包含一套我工作中常用的模板库,及不需要MFC支持的excel操作接口,导出函数调用栈(dump stack)接口,可以直接用VS2008运行TestCodeLib.sln来根据unit test来了解用法。 ⑴ 需求(requirements) 重量级的BOOST非常强大,但有时候项目中没有引入它,这时候我们需要自己的模板库。 BOOST is very powerful, but some projects have not include BOOST library. So we need out own template type trait library -- it is the responsibility of this lightweight library. 即使BOOST非常强大,但有些常用的功能其也没有,而经常性的代码中又需要这些功能。比如把运行期数据转换为元程序需要的编译期数据。 Even if BOOST is very powerful,it can't still meet all requirements. e.g. convert runtime data into compile period data needed by metaprogramming. /*************************************************************************************************************************************/ ⑵ 益处(advantage) 此泛型库抽象了一些常用的业务需求,可以避免大量的重复工作。 它是完全泛型的并且是类型安全的(没有强制类型转换),如果使用错误将导致编译失败,从而提高了正确率(正确性由编译器保证)。 这个库的很多模板类型推导不需要C++11的支持,这是一个大的优势(VS2010才开始支持C++11)。 this general library draws out some common business and avoid unnecessary repeat work. it is completed general and type-safe(without any type cast), mistake(s) will cause compile failure, so it improves correctness. In this library , type deduce need't C++11's support, it is big advantage. (VS2010 begin to support C++11) /*************************************************************************************************************************************/ ⑶ 用法(usage) 下载这个库后,使用VS打开.\CodeLib\testcase\TestCodeLib\TestCodeLib.sln,直接按F5启动,即可以看到许多单元测试的用法/测试用例的输出。 如果需要使用某功能,可以参考其对应的测试代码的用法。(每个功能文件.\CodeLib\include\MiniMPL\xxx.hpp,都对应一个测试文件.\CodeLib\testcase\MiniMPL\test_xxx.hpp) (这个库的使用及修改是完全自由的,只需要保留文件头中的注释即可) usage: download this library, open .\CodeLib\testcase\TestCodeLib\TestCodeLib.sln with VS,you can see many usage/test output of unit test. every feature has according unit test file, it shows its usage. e.g. .\CodeLib\include\MiniMPL\xxx.hpp has according ".\CodeLib\testcase\MiniMPL\test_xxx.hpp" this library is all free, the only requirement is that you need to keep the comments in header file. /*********************************************************************************************************************************************************/ ⑷ 本库提供的主要功能介绍: major feature in this lib: ◆ [typeTraits.hpp] ★ 测试类型的基本属性,比如IsConst/IsVoliate/IsRef/isAtomType/isBuildInType/isEnumType/IsIterator/IsPointer/isString/isInnerFloat/isArray/IsBaseDerive/.... ★ 转换类型的基本属性,比如AddConst/AddVoliate/AddRef/AddPointer,..,RemoveConst/RemoveVoliate/RemoveRef/RemovePointer,... 这类功能是元程序库的基本支持组件,其它库(比如boost)也提供了,但本库时提供的检测属性更多。 ☆ detect type property. e.g.IsConst/IsVoliate/IsRef/isAtomType/isBuildInType/isEnumType/IsIterator/IsPointer/isString/isInnerFloat/isArray/IsBaseDerive/.... ☆ convert type basic qualifier,e.g. AddConst/AddVoliate/AddRef/AddPointer,..,RemoveConst/RemoveVoliate/RemoveRef/RemovePointer,... get type traits.e.g.const/voliate/ref/isAtomType/isBuildInType/isEnumType/isString/isInnerFloat/isArray/IsBaseDerive/.... It is base support component of metaprogramming system,it is similiar with BOOST , but this lib provide more. ◆ [typeConvert.hpp] ★ 实现类型的修饰符转换。比如让输出参数类型的修饰符(const/voliate/ref/*)和输入参数类型的修饰符一样。 SameConst/SameVoliate/SameRef/SamePointer/SameAllQualifier/RefAdapter 应用场景:存取结构体的某类成员,当输入参数有某种const/voliate/ref修饰符时,通常要求返回值也有类似的修饰符。 ★ 当把"智能指针/stl迭代器/C指针/前三者嵌套"都视为指针时,其内的最终值(非指针值)是一致的,在模板函数中,某些场景需要取得其最终的非指针值。 应用场景:转发模板函数,如 template<typename T> void transmit(T p) { receive(p); } //void receive(int&); 如果transmit的传入实参p为指针类型(比如smartpointer<vector<int*>::iterator>*或者vector<int*>::iterator), 但是转发的接收函数receive的形参为非指针类型(比如int&),理论上是可以实现转换的。 Get::finalValue接口提供了这种自动的转: template<typename T> void transmit(T p) { receive(Get::finalValue(p)); } ☆ Convert type qualifiers,e.g. addConst/removeConst.. , keep same output qualifier (const/voliate/ref/*) with input type. apply scenario: get member of one structure object. ☆ Think "stlSmartptr<T>/StlContainer<T>::iterator/T*" as pointer, their inner non-pointer value is same. in some scenario, the final non-pointer value is needed. e.g. template<typename T> void transmit(T p) { receive(p); } //void receive(int&); if real paremeter "p" is smartpointer<vector<int*>::iterator>* or vector<int*>::iterator , but needed parameter by "receive" is int&, in theory it is OK. Get::finalValue provide this conversion: template<typename T> void transmit(T p) { receive(Get::finalValue(p)); } ◆ [traverseTypeSet.hpp] ★ C++语法不支持模板函数/模板成员函数作为回调函数。本库采用了封装,可以支持模板函数的回调,并且支持最多7个可变参数(可以简易扩充参数个数)。 可以遍历一个TypeList或者枚举值范围CEnumRange,然后以满足条件的类型回调用户的模板函数。 其广泛的应用场景即是把运行期数据以一种非hard-code的方式转化为编译期数据,从而满足元程序对编译期数据的需求。 ☆ C++ doesn't support template-based callback function. this lib package support template-based callback function(MAX 7 various parameters,easy to expand). It can traverse one TypeList or enum value , then call user's template function by suitable type/enum value. This feature converts runtime data into compile data to meet metaprogramming requirement without hard-code way, it is one big advantage. ◆ [functionTraits.hpp] ★ 获取任意类型函数的各种特征,比如函数的所有参数Params_T,返回值类型Return_T,对象类型Object_T(如果是成员函数),第N个参数的类型GetFunctionParam<F,N>, 这些类型都是包含修饰符(const/voliate/ref)的完整类型。 这些组件对于操作函数非常重要。 ☆ get some traits of any function, include all parameter type "Params_T",return type "Return_T", host type "Object_T"(if member-function) , No.x parameter type "GetFunctionParam<F,x>". this type include all signature qualifiers. This component is very important for metaprogramming based on function. ◆ 有时候STL的算法并不好用,经常是为了第三个参数需要自己写一个专用的琐碎的小函数。 虽然可以用std的bind或者boost的lambda,但是对于某些嵌套情况,用起来非常麻烦,这个库提供了下面的一些解决方式: sometimes STL algorithm is not good and it needs one traival function object(third parameter) , althrough std::bind/boost::lambda is available, but for some nest case, it is very hard to be used.this library provide below features: [function.hpp] ★ 把既有的多元函数转换为一元函数对象UnaryFunction。它通常应用于泛型(比较/排序/遍历)算法的第三个参数。 ☆ convert existing multi-parameters into unary function, it is general used as 3rd parameter in general algorithm. e.g. stl::for_each [functionobject.hpp] ★ 把一些常用目的的函数封装成函数对象,比如"比较器/测试器" ☆ function object with special abstract targart. e.g. "comparer/Tester" ◆ [functionCreater.hpp] ★ 把多元函数封装为一元函数的帮助函数。(一元函数对象的类型通常不易于书写) ☆ helper function to pack multi-parameters into unary function.(it is hard to write unary function object type) ◆ [paramPackage.hpp] ★ 实现了把任意多个(最多7个,可简易扩充),任意类型的参数封装成一个参数以利于数据传递。 ☆ pack any number parameter (max 7,easy expand) into one parameter . it is easy to transfer. ◆ [classregister.hpp] ★ MFC的动态创建不是泛型的,创造出来的对象必须是CObject的派生类,而且支持的创造方式单一,不够灵活有时候甚至不能满足需求。 本库里提供了一个泛型的动态创建方式,可以以多种灵活的方式甚至用户自定义的方式来匿名动态创建对象,创建的对象基类可以由用户指定(必须存在派生关系)。 ☆ like MFC's DYNAMIC_CREATE, but the one of MFC is not general,the instance MUST be drived from class CObject, MFC dynamic creation has only one create way,sometimes it is not enough。 this library provides general dynamic create way, can create object by multiple ways , even customized way.and base class can be specified by user. ◆ [callbackWorker.hpp] ★ 最易于使用的回调函数是无参数的回调函数。 此功能可以把任意多个参数的多元(成员/非成员)函数封装成一个无参数函数,作为简单的回调函数。 ☆ best callback function is non-parameter function. This feature packs multiple-parameter function into one no-parameter function, it is easy to be used as callback function. ◆ [memberPtr.hpp] ★ 以统一的方式实现了任意级数的结构体成员的存和取,比如多级结构体嵌套。例子:a.m_b.m_c.m_d.....m_x,非常易于在模板设计中使用。 ☆ access any level member of structure object by one unified way. e.g:a.m_b.m_c.m_d.....m_x,it is easy to be used in template componment. ◆ [anyObject.hpp] ★ 任意对象类(CAnyObject)。提供模板化的指针操作符,如果不支持用户指定指针类型,则转换结果为NULL,从而保证正确性。 ☆ package any object(CAnyObject), it operator function is template-based. if it doesn't support conversion, it return NULL. ◆ [dataset.hpp] ★ 把STL容器和经典数组封装成统一的形式,在使用上不再区别对待。对于C数组,将会自动检测越界情况。 ★ 可以使用初始化列表对数组,STL容器进行(反复)初始化。例如:vector<int> a={1,2,3,45,2}; ☆ pack STL container and class array into unified object with several same interfaces. ☆ can initialize array/stl container with initalization list repeated. e.g. vector<int> a={1,2,3,45,2}; ◆ [macroLoop.hpp] ★ 当多条语句的差别仅仅是一个数字时,可以利用提供的循环宏简化成一条宏语句,从而简化书写。用法可参见对应的单元测试例子。 ☆ if only one number is different in multiple statements, can use one macro loop to simplify them (one macro statement) usage refer to unit test. ◆ [mathOperator.hpp] ★ 泛型的数学操作符。"equal/lesser/NotBinary/NotUnary/notEqual/lesserEqual/greater/greaterEqual及交换函数swap/swapif" ☆ general math operator. "equal/lesser/NotBinary/NotUnary/notEqual/lesserEqual/greater/greaterEqual and swap/swapif" /*************************************************************************************************************************************/ ⑸ 感谢及借鉴: 本库中的占位符[placeHolder.hpp]借鉴于boost库,感谢boost库的大师们的灵感。 typelist来自loki库,但是把命名空间Loki改为MiniMPL以避免频繁的命名域切入/切出,感谢Andrei Alexandrescu的精彩演绎与启发. thanks and borrow: Args [placeHolder.h] comes from BOOST::MPL. thanks for BOOST team. typelist comes from loki lib with tiny modification(rename namespace loki to MiniMPL to avoid field switch frequently).thanks for Andrei Alexandrescu
Visual C++ 6.0 专业便携版 1.仅用于技术学习交流之用,禁止用于一切商业用途。 2.对于使用本人的软件所造成的一切损失及后果由使用者自行承担。 3.使用前先将压缩包解压到磁盘上,运行“安装.exe”开始初始化环境配置,这个过程仅在首次使用时执行一次。 4.安装后会生成“unins000.exe”和“unins000.dat”前者是卸载程序,后者保存着卸载数据,请勿删除。 5.需要卸载时,直接运行“unins000.exe”即开始卸载。 对原版的修改: 1.解除了原版不能打开超过 256 色位图的限制。 2.解除了原版字体选项过少的限制。 3.增加对 .asm 和 .inc 汇编类型文件的支持,向项目添加此类文件时,会被默认归类到“Source Files”和“Header Files”分类下。 4.替换 c2.dll 版本为 13.0.9044.0 ,支持内联汇编的 SSE 指令。 5.修改 VC6 版本号为 "6.x" ,使同一系统中能和其他版本的 VC6 共存。 增加的内容: 1.集成 Visual Assist X、WndTabs 插件。 2.集成 DevTrueColour.dll 和 FileTool.dll 插件,前者是字体颜色选择插件,后者是用于 Vista 以上系统中用来替代 IDE 本身的添加和打开文件功能,以防止 IDE 崩溃。 3.集成 VS 2008 SP1 的 ml.exe ,增加对汇编的支持。 4.集成 Glut 3.7 beta。 5.集成 企业版核心组件。 SDK 集成: 1.集成 Windows 2003 SDK ,这是微软官方最后一个支持 VC 6 的 SDK 版本。 2.集成 WTL 和 VFC 。 3.集成 STL Port、SGI STL 两者任选其一,可用来代替 VC 6 自带的 STL 以获得更好的性能。 4.集成 Native Development Kit 。 5.集成 Masm32 ,方便编写和调试简单的汇编程序。 6.集成 Boost C++ 库。 编译器集成: 1.集成 Intel C++ 10 编译器,这是能支持 VC6 的最后一个 Intel C++ 编译器。 by cntrump December 25, 2010
Visual C++ 6.0 专业便携版 1.仅用于技术学习交流之用,禁止用于一切商业用途。 2.对于使用本人的软件所造成的一切损失及后果由使用者自行承担。 3.使用前先将压缩包解压到磁盘上,运行“安装.exe”开始初始化环境配置,这个过程仅在首次使用时执行一次。 4.安装后会生成“unins000.exe”和“unins000.dat”前者是卸载程序,后者保存着卸载数据,请勿删除。 5.需要卸载时,直接运行“unins000.exe”即开始卸载。 对原版的修改: 1.解除了原版不能打开超过 256 色位图的限制。 2.解除了原版字体选项过少的限制。 3.增加对 .asm 和 .inc 汇编类型文件的支持,向项目添加此类文件时,会被默认归类到“Source Files”和“Header Files”分类下。 4.替换 c2.dll 版本为 13.0.9044.0 ,支持内联汇编的 SSE 指令。 5.修改 VC6 版本号为 "6.x" ,使同一系统中能和其他版本的 VC6 共存。 增加的内容: 1.集成 Visual Assist X、WndTabs 插件。 2.集成 DevTrueColour.dll 和 FileTool.dll 插件,前者是字体颜色选择插件,后者是用于 Vista 以上系统中用来替代 IDE 本身的添加和打开文件功能,以防止 IDE 崩溃。 3.集成 VS 2008 SP1 的 ml.exe ,增加对汇编的支持。 4.集成 Glut 3.7 beta。 5.集成 企业版核心组件。 SDK 集成: 1.集成 Windows 2003 SDK ,这是微软官方最后一个支持 VC 6 的 SDK 版本。 2.集成 WTL 和 VFC 。 3.集成 STL Port、SGI STL 两者任选其一,可用来代替 VC 6 自带的 STL 以获得更好的性能。 4.集成 Native Development Kit 。 5.集成 Masm32 ,方便编写和调试简单的汇编程序。 6.集成 Boost C++ 库。 编译器集成: 1.集成 Intel C++ 10 编译器,这是能支持 VC6 的最后一个 Intel C++ 编译器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值