C/C++ 中判斷某一文件或目錄是否存在

 

方法一:C++中比較簡單的一種辦法(使用文件流打開文件)ios

 1 #include <iostream>
 2 #include <fstream>
 3 
 4 using namespace std;
 5  
 6 #define FILENAME "*.dat"  // 指定文件名
 7 
 8 int main( void )
 9 {
10      fstream _file;
11      _file.open(FILENAME, ios::in);
12      if(!_file)
13      {
14           cout<<FILENAME<<"沒有被建立!"<<endl;
15      }
16      else
17      {
18           cout<<FILENAME<<"已經存在!"<<endl;
19      }
20 
21      cin.get();
22      return 0;
23 }

方法二:利用C語言庫函數(_accesswindows

函數原型函數

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

函數參數spa

  l  path:文件路徑code

  l  mode:讀寫屬性blog

返回值(MSDN)ci

       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:get

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", 0 )) != -1 )
   {
      printf( "File ACCESS.C exists " );
      /* Check for write permission */
      if( (_access( "ACCESS.C", 2 )) != -1 )
         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[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  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 = 0;
    //指定路徑           
    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 -1;
        }
    }
    strFilePath  =  full_path.native_directory_string();
    return 0;
}
相關文章
相關標籤/搜索