<pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

          <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

          <p id="bbfd9"></p>

          <p id="bbfd9"><cite id="bbfd9"></cite></p>

            <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

            <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
            <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>

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

            時間:2025-12-05 16:04:29 C語言

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

              不同的編程語言判斷文件是否存在的方法是不一樣的。你知道C/C++中如何判斷某一文件或目錄是否存在嗎?下面是小編為大家帶來的C/C++中如何判斷某一文件或目錄是否存在的知識,歡迎閱讀。

              1.C++很簡單的一種辦法:

              復制代碼 代碼如下:

              #include

              #include

              using namespace std;

              #define FILENAME "stat.dat"

              int main()

              {

              fstream _file;

              _file.open(FILENAME,ios::in);

              if(!_file)

              {

              cout<

              }

              else

              {

              cout<

              }

              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

              #include

              #include

              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)檢查某一目錄是否存在:

              復制代碼 代碼如下:

              /pic/目錄是否存在的檢查:

              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

              #include

              #include

              int GetFilePath(std::string &strFilePath)

              {

              string strPath;

              int nRes = 0;

              /pic/p>

              strPath = "D:/myTest/Test1/Test2";

              namespace fs = boost::filesystem;

              /pic/p>

              fs::path full_path( fs::initial_path() );

              full_path = fs::system_complete( fs::path(strPath, fs::native ) );

              /pic/p>

              if ( !fs::exists( full_path ) )

              {

              /pic/p>

              bool bRet = fs::create_directories(full_path);

              if (false == bRet)

              {

              return -1;

              }

              }

              strFilePath = full_path.native_directory_string();

              return 0;

              }


            【C/C++中如何判斷某一文件或目錄是否存在】相關文章:

            C/C++如何獲取目錄下的文件列表信息06-25

            如何在C/C++中調用Java03-13

            如何判斷PHP文件上傳file是否已選擇上傳文件11-06

            C語言/C++中如何產生隨機數02-08

            在C語言/C++中如何產生隨機數03-18

            WPS如何刪除文件中某一頁的頁眉11-19

            如何運行C++程序11-08

            php中目錄文件操作詳談02-21

            linux中目錄與文件權限的意義11-30

                    <pre id="bbfd9"><del id="bbfd9"><dfn id="bbfd9"></dfn></del></pre>

                    <ruby id="bbfd9"></ruby><p id="bbfd9"><mark id="bbfd9"></mark></p>

                    <p id="bbfd9"></p>

                    <p id="bbfd9"><cite id="bbfd9"></cite></p>

                      <th id="bbfd9"><form id="bbfd9"><dl id="bbfd9"></dl></form></th>

                      <p id="bbfd9"><cite id="bbfd9"></cite></p><p id="bbfd9"></p>
                      <p id="bbfd9"><cite id="bbfd9"><progress id="bbfd9"></progress></cite></p>
                      飘沙影院