導航:首頁 > 文件類型 > vc文件夾文件是否存在

vc文件夾文件是否存在

發布時間:2024-01-15 22:47:07

Ⅰ VC++如何檢測文件是否存在

VC++ 判斷文件是否存在的方法有:

1. 使用_access函數,函數原型為:
int _access( const char *path, int mode );

2. 使用CreateFile函數,函數原型為:
HANDLE CreateFile( LPCTSTR lpFileName, //pointer to name of the file DWORD dwDesiredAccess, // access (readwrite) modeDWORD dwShareMode, // share mode LPSECURITY_, // pointer to security attributes DWORD dwCreationDisposition, //how to create DWORD dwFlagsAndAttributes, // file attributes HANDLE hTemplateFile //handle to file with attributes to // );

3. 使用FindFirstFile函數,函數原型為:
HANDLE FindFirstFile( LPCTSTR lpFileName, //pointer to name of file to search for LPWIN32_FIND_DATA lpFindFileData // pointer to returned information );

4. 使用GetFileAttributes函數,函數原型如下:

DWORD GetFileAttributes( LPCTSTRlpFileName // pointer to the name of a file or directory );

5. 使用Shell Lightweight Utility APIs函數。
PathFileExists()專門判斷文件和目錄時否存在的函數文件名可讀性比較強還可以判斷目錄是否存在Header: Declared in Shlwapi.h ,Import Library: Shlwapi.lib 。
方法1:
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
void main( void ) {
if( (_access( "D:\\a.txt", 0 )) != -1 ) {
printf( "File ACCESS.C exists\n" );
if( (_access( "ACCESS.C", 2 )) != -1 )
printf( "File ACCESS.C has write permission\n" ); } }
方法2:
if (INVALID_HANDLE_VALUE != CreateFile("D:\\a.txt", GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) {
AfxMessageBox("File ACCESS.C exists\n"); }
方法3:

#define _WIN32_WINNT 0x0400
#include <windows.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
printf ("Target file is %s.\n", argv[1]);
hFind = FindFirstFile(argv[1], &FindFileData);
if (hFind == INVALID_HANDLE_VALUE) {
printf ("Invalid File Handle. GetLastError reports %d\n", GetLastError ()); return (0); }
else {
printf ("The first file found is %s\n", FindFileData.cFileName); FindClose(hFind);
return (1); } }
方法4:
if (GetFileAttributes("c:\\1.txt") == -1)
MessageBox(0."Invalid File ","hehe",0)
else
MessageBox(0."The first file found ","haha",0)
方法5:
if (INVALID_HANDLE_VALUE != CreateFile("D:\\a.txt", 0, 0, NULL, OPEN_EXISTING, 0, NULL)) {
AfxMessageBox("File exists\n");
}

Ⅱ vc判斷文件是否存在

方法一:PathFileExists(FilePath); 返回true則存在,返回false則不存在,注意要加上以下代碼

#include<shlwapi.h>
#pragmacomment(lib,"Shlwapi.lib")

方法二:CFile::GetStatus(WMSIniFilePath,filestatus),返回true則存在,返回false則不存在

參數:

rStatus:

A reference to a user-supplied CFileStatus structure that will receive the status information. The CFileStatus structure has the following fields:

CTime m_ctimeThe date and time the file was created.

CTime m_mtimeThe date and time the file was last modified.

CTime m_atimeThe date and time the file was last accessed for reading.

LONG m_sizeThe logical size of the file in bytes, as reported by the DIR command.

BYTE m_attributeThe attribute byte of the file.

char m_szFullName[_MAX_PATH]The absolute filename in the Windows character set.

lpszFileName:

A string in the Windows character set that is the path to the desired file. The path can be relative or absolute, but cannot contain a network name.

參考:http://msdn.microsoft.com/zh-cn/aa270504

Ⅲ VC環境下如何檢測指定文件夾中是否存在某個文件

CreateFile打開,成功當然存在,失敗再用GetLastError看原因,根據失敗原因判斷文件是否存在。

另外,CFileFind應該沒什麼問題。可參考:
CFileFind finder;
BOOL bWorking = finder.FindFile(_T("*.*"));
while (bWorking)
{
bWorking = finder.FindNextFile();
TRACE(_T("%s\n"), (LPCTSTR)finder.GetFileName());
}

閱讀全文

與vc文件夾文件是否存在相關的資料

熱點內容
怎麼用編程畫小花 瀏覽:65
php文件如何下載文件 瀏覽:614
javacapsule 瀏覽:20
extjs按鈕垂直居中 瀏覽:163
ibjsx 瀏覽:647
直銷可編程直流電源哪裡買 瀏覽:952
蘋果6qq錄音文件聽不了 瀏覽:6
網路線怎麼拔 瀏覽:328
webclip文件有什麼危害 瀏覽:700
創維32e360e怎麼看網路電視 瀏覽:824
js網站載入動畫 瀏覽:411
shelljava 瀏覽:666
系統文件被刪可以還原嗎 瀏覽:835
萬方等網站怎麼下載 瀏覽:857
為什麼電腦文件顯示是寫字板 瀏覽:37
百度網盤打卡壓縮文件 瀏覽:925
英語怎麼讀的網站 瀏覽:115
怎麼遠程改電腦開機密碼 瀏覽:501
可以直接打開壓縮文件的瀏覽器 瀏覽:654
qq相冊一個文件可以存多少張照片 瀏覽:894

友情鏈接