欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2013年07月13日 11:04:21   作者:  
以下文章是對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<<"沒有被創(chuàng)建";
      }
      else
      {
          cout<<FILENAME<<"已經存在";
      }
      return 0;
}

2.利用 c 語言的庫的辦法:
函數名: access
功  能: 確定文件的訪問權限
用  法: int access(const char *filename, int amode);
以前一直沒用過這個函數,今天調試程序發(fā)現了這個函數,感覺挺好用,尤其是判斷一個文件或文件夾是否存在的時候,用不著再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 ) );
    //判斷各級子目錄是否存在,不存在則需要創(chuàng)建
    if ( !fs::exists( full_path ) )
    {
        // 創(chuàng)建多層子目錄
        bool bRet = fs::create_directories(full_path);
        if (false == bRet)
        {
            return -1;
        }
    }
    strFilePath = full_path.native_directory_string();
    return 0;
}

相關文章

  • C++基于prim實現迷宮生成

    C++基于prim實現迷宮生成

    這篇文章主要為大家詳細介紹了C++基于prim實現迷宮生成,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • C++ Log日志類輕量級支持格式化輸出變量實現代碼

    C++ Log日志類輕量級支持格式化輸出變量實現代碼

    這篇文章主要介紹了C++ Log日志類輕量級支持格式化輸出變量實現代碼,需要的朋友可以參考下
    2019-04-04
  • C++版圖書管理系統

    C++版圖書管理系統

    這篇文章主要為大家詳細介紹了C++版圖書管理系統,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • vs2019 MFC實現office界面的畫圖小項目

    vs2019 MFC實現office界面的畫圖小項目

    本文主要介紹了vs2019 MFC實現office界面的畫圖小項目,對大家入門有一定的幫助,需要的朋友們下面隨著小編來一起學習學習吧
    2021-06-06
  • C++實現并優(yōu)化異常系統

    C++實現并優(yōu)化異常系統

    異常處理是C++的一項語言機制,用于在程序中處理異常事件,下面這篇文章主要給大家介紹了關于C++中異常的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-08-08
  • OpenCV使用GrabCut實現摳圖功能

    OpenCV使用GrabCut實現摳圖功能

    Grabcut是基于圖割(graph cut)實現的圖像分割算法,它需要用戶輸入一個bounding box作為分割目標位置,實現對目標與背景的分離/分割。本文將使用GrabCut實現摳圖功能,需要的可以參考一下
    2023-02-02
  • 詳解C++編程中對二進制文件的讀寫操作

    詳解C++編程中對二進制文件的讀寫操作

    這篇文章主要介紹了C++編程中對二進制文件的讀寫操作,是C++入門學習中的基礎知識,需要的朋友可以參考下
    2015-09-09
  • 詳細講解C語言中的數據以及位運算

    詳細講解C語言中的數據以及位運算

    這篇文章主要為大家詳細介紹了C語言中數據表示方法以及位運算的相關知識點,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-06-06
  • 解決Devc++運行窗口中文亂碼的實現步驟

    解決Devc++運行窗口中文亂碼的實現步驟

    本文主要介紹了如何解決Devc++運行窗口中文亂碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-06-06
  • C語言貪吃蛇經典小游戲

    C語言貪吃蛇經典小游戲

    這篇文章主要為大家詳細介紹了C語言貪吃蛇經典小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01

最新評論