[轉貼]檢查某檔案是否存在的方法

有關於C/C++的語法, 程式等
回覆文章
頭像
tim
文章: 1380
註冊時間: 2008年 11月 26日, 00:49

[轉貼]檢查某檔案是否存在的方法

文章 tim »

轉貼自:
http://members.lycos.co.uk/nirsoft/vc/isfileexist.html

一共有三個方法, 請參考:

IsFileExist1: We try to open the file, if we get a valid hanle, the file exists.
IsFileExist2: We try to find the file by using the FindFirstFile API function. if we get a valid hanle, the file exists.
IsFileExist3: We try to get the attributes of the file. If the function fails, and returns 0xffffffff value, the file doesn't exist.

BOOL IsFileExist1(LPSTR lpszFilename)
{
HANDLE hFile = CreateFile(lpszFilename, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hFile != INVALID_HANDLE_value)
{
CloseHandle(hFile);
return TRUE;
} else
{
return FALSE;
}
}


BOOL IsFileExist2(LPSTR lpszFilename)
{
WIN32_FIND_DATA wfd;

HANDLE hFind = FindFirstFile(lpszFilename, &wfd);
if (hFind == INVALID_HANDLE_value)
return FALSE;
else
{
FindClose(hFind);
return TRUE;
}
}

BOOL IsFileExist3(LPSTR lpszFilename)
{
DWORD dwAttr = GetFileAttributes(lpszFilename);
if (dwAttr == 0xffffffff)
return FALSE;
else
return TRUE;
}
多多留言, 整理文章, 把經驗累積下來.....
回覆文章