织梦CMS - 轻松建站从此开始!

罗索

C/C++中判断某一文件或目录是否存在

落鹤生 发布于 2015-04-23 10:37 点击:次 
1.C++很简单的一种办法: #includeiostream #includefstream using namespace std; #defineFILENAMEstat.dat int main() { fstream_file; _file.open(FILENAME,ios::in); if (!_file) { coutFILENAME 没有被创建 ; } else { coutFILENAME 已经存在 ; } return 0; } 2.
TAG: 判断文件是否存在  

1.C++很简单的一种办法:

  1. #include <iostream> 
  2. #include <fstream> 
  3. using namespace std; 
  4. #define FILENAME "stat.dat" 
  5. int main() 
  6.      fstream _file; 
  7.      _file.open(FILENAME,ios::in); 
  8.      if(!_file) 
  9.      { 
  10.          cout<<FILENAME<<"没有被创建"
  11.       } 
  12.       else 
  13.       { 
  14.           cout<<FILENAME<<"已经存在"
  15.       } 
  16.       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

  1. /* ACCESS.C: This example uses _access to check the 
  2.  * file named "ACCESS.C" to see if it exists and if 
  3.  * writing is allowed. 
  4.  */ 
  5.  
  6. #include  <io.h> 
  7. #include  <stdio.h> 
  8. #include  <stdlib.h> 
  9.  
  10. void main( void ) 
  11.    /* Check for existence */ 
  12.    if( (_access( "ACCESS.C", 0 )) != -1 ) 
  13.    { 
  14.       printf( "File ACCESS.C exists " ); 
  15.       /* Check for write permission */ 
  16.       if( (_access( "ACCESS.C", 2 )) != -1 ) 
  17.          printf( "File ACCESS.C has write permission " ); 
  18.    } 

Output

File ACCESS.C existsFile ACCESS.C has write permission

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

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

  1. #define _WIN32_WINNT 0x0400 
  2.  
  3. #include "windows.h" 
  4.  
  5. int 
  6. main(int argc, char *argv[]) 
  7.   WIN32_FIND_DATA FindFileData; 
  8.   HANDLE hFind; 
  9.  
  10.   printf ("Target file is %s. ", argv[1]); 
  11.  
  12.   hFind = FindFirstFile(argv[1], &FindFileData); 
  13.  
  14.   if (hFind == INVALID_HANDLE_VALUE) { 
  15.     printf ("Invalid File Handle. Get Last Error reports %d ", GetLastError ()); 
  16.   } else { 
  17.     printf ("The first file found is %s ", FindFileData.cFileName); 
  18.     FindClose(hFind); 
  19.   } 
  20.  
  21.   return (0); 

(2)检查某一目录是否存在:

  1. ///目录是否存在的检查: 
  2. bool  CheckFolderExist(const string &strPath) 
  3.     WIN32_FIND_DATA  wfd; 
  4.     bool rValue = false
  5.     HANDLE hFind = FindFirstFile(strPath.c_str(), &wfd); 
  6.     if ((hFind != INVALID_HANDLE_VALUE)
  7.  && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) 
  8.     { 
  9.         rValue = true;   
  10.     } 
  11.     FindClose(hFind); 
  12.     return rValue; 

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

  1. #include <boost/filesystem/operations.hpp> 
  2. #include <boost/filesystem/path.hpp> 
  3. #include <boost/filesystem/convenience.hpp> 
  4.  
  5. int GetFilePath(std::string &strFilePath) 
  6.     string strPath; 
  7.     int nRes = 0; 
  8.  
  9.     //指定路径            
  10.     strPath = "D:/myTest/Test1/Test2"
  11.     namespace fs = boost::filesystem; 
  12.  
  13.     //路径的可移植 
  14.     fs::path full_path( fs::initial_path() ); 
  15.     full_path = fs::system_complete( fs::path(strPath, fs::native ) ); 
  16.     //判断各级子目录是否存在,不存在则需要创建 
  17.     if ( !fs::exists( full_path ) ) 
  18.     { 
  19.         // 创建多层子目录 
  20.         bool bRet = fs::create_directories(full_path); 
  21.         if (false == bRet) 
  22.         { 
  23.             return -1; 
  24.         } 
  25.  
  26.     } 
  27.     strFilePath = full_path.native_directory_string(); 
  28.  
  29.     return 0; 
(roger_77)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201504/17289.html]
本文出处:CSDN博客 作者:roger_77 原文
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容