国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
C/C++中判斷某一文件或目錄是否存在
C/C++中判斷某一文件或目錄是否存在
 
1.C++很簡(jiǎn)單的一種辦法:
#include <iostream>
#include 
<fstream>
using namespace std;
#define FILENAME "stat.dat"
int main()
{
     fstream _file;
     _file.open(FILENAME,ios::
in);
     
if(!_file)
     {
         cout
<<FILENAME<<"沒(méi)有被創(chuàng)建";
      }
      
else
      {
          cout
<<FILENAME<<"已經(jīng)存在";
      }
      
return 0;
}

 

2.利用 c 語(yǔ)言的庫(kù)的辦法:

函數(shù)名: access
功  能: 確定文件的訪問(wèn)權(quán)限
用  法: int access(const char *filename, int amode);
以前一直沒(méi)用過(guò)這個(gè)函數(shù),今天調(diào)試程序發(fā)現(xiàn)了這個(gè)函數(shù),感覺(jué)挺好用,尤其是判斷一個(gè)文件或文件夾是否存在的時(shí)候,用不著再find了,文件的話還可以檢測(cè)讀寫(xiě)權(quán)限,文件夾的話則只能判斷是否存在,下面摘自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 " );
   }
}
Output
File ACCESS.C existsFile ACCESS.C has write permission

 

3.在windows平臺(tái)下用API函數(shù)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;   
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
關(guān)于C++中如何判斷文件,目錄存在的若干方法
長(zhǎng)短路徑轉(zhuǎn)換
VC文件目錄常見(jiàn)操作
在Windows下,如何遍歷某目錄里所有文件?
VC 常見(jiàn)文件操作
C語(yǔ)言編程之怎樣判斷某一文件是否存在
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服