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

 1 //1.C++很简单的一种办法:
 2 #include <iostream>
 3 #include <fstream>
 4 using namespace std;
 5 #define FILENAME "stat.dat"
 6 int main()
 7 {
 8      fstream _file;
 9      _file.open(FILENAME,ios::in);
10      if(!_file)
11      {
12          cout<<FILENAME<<"没有被创建";
13       }
14       else
15       {
16           cout<<FILENAME<<"已经存在";
17       }
18       return 0;
19 }
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 
 1 //Example
 2 /* ACCESS.C: This example uses _access to check the * file named "ACCESS.C" to see if it exists and if * writing is allowed. */
 3 #include  <io.h>
 4 #include  <stdio.h>
 5 #include  <stdlib.h>
 6 void main( void )
 7 {  
 8      /* Check for existence */   
 9      if( (_access( "ACCESS.C", 0 )) != -1 )   
10      {      printf( "File ACCESS.C exists " );     
11            /* Check for write permission */     
12            if( (_access( "ACCESS.C", 2 )) != -1 )         
13           printf( "File ACCESS.C has write permission " );   
14      }
15 }
16 
17 /*
18 Output
19 File ACCESS.C existsFile ACCESS.C has write permission 
20 */
 1 //3.在windows平台下用API函数FindFirstFile(...):
 2 
 3 //(1)检查文件是否存在:
 4 #define _WIN32_WINNT 0x0400
 5 
 6 #include "windows.h"
 7 
 8 int main(int argc, char *argv[])
 9 {
10   WIN32_FIND_DATA FindFileData;
11   HANDLE hFind;
12 
13   printf ("Target file is %s. ", argv[1]);
14 
15   hFind = FindFirstFile(argv[1], &FindFileData);
16 
17   if (hFind == INVALID_HANDLE_VALUE) {
18     printf ("Invalid File Handle. Get Last Error reports %d ", GetLastError ());
19   } else {
20     printf ("The first file found is %s ", FindFileData.cFileName);
21     FindClose(hFind);
22   }
23 
24   return (0);
25 }
 1 ///2目录是否存在的检查:
 2 bool  CheckFolderExist(const string &strPath)
 3 {
 4     WIN32_FIND_DATA  wfd;
 5     bool rValue = false;
 6     HANDLE hFind = FindFirstFile(strPath.c_str(), &wfd);
 7     if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
 8     {
 9         rValue = true;   
10     }
11     FindClose(hFind);
12     return rValue;
13 }
 1 //4.使用boost的filesystem类库的exists函数
 2 #include <boost/filesystem/operations.hpp>
 3 #include <boost/filesystem/path.hpp>
 4 #include <boost/filesystem/convenience.hpp>
 5 
 6 int GetFilePath(std::string &strFilePath)
 7 {
 8     string strPath;
 9     int nRes = 0;
10 
11     //指定路径            
12     strPath = "D:/myTest/Test1/Test2";
13     namespace fs = boost::filesystem;
14 
15     //路径的可移植
16     fs::path full_path( fs::initial_path() );
17     full_path = fs::system_complete( fs::path(strPath, fs::native ) );
18     //判断各级子目录是否存在,不存在则需要创建
19     if ( !fs::exists( full_path ) )
20     {
21         // 创建多层子目录
22         bool bRet = fs::create_directories(full_path);
23         if (false == bRet)
24         {
25             return -1;
26         }
27 
28     }
29     strFilePath = full_path.native_directory_string();
30 
31     return 0;
32 }

 

 

posted @ 2014-06-19 15:41  MATU  阅读(2040)  评论(0编辑  收藏  举报