程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C/C++中若何斷定某一文件或目次能否存在

C/C++中若何斷定某一文件或目次能否存在

編輯:關於C++

C/C++中若何斷定某一文件或目次能否存在。本站提示廣大學習愛好者:(C/C++中若何斷定某一文件或目次能否存在)文章只能為提供參考,不一定能成為您想要的結果。以下是C/C++中若何斷定某一文件或目次能否存在正文


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 " );
   }
}

OutputFile 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/Test1/Test2";
    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;
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved