檢測文件存在的四種方法。test if a File exist or not?
1.
WIN32_FIND_DATA m_data;
HANDLE hFile;
hFile=FindFirstFile(filename,&m_data)
if(hFile==INVALID_HANDLE_VALUE) //file not found
Make sure you close the handle if the file is found.
FindClose(hFile);
2.
You can use SHGetFileInfo()
The prototype of the function is as follows:
3.
PathFileExists
4. 加一個標準c庫函數
WIN32_FIND_DATA m_data;
HANDLE hFile;
hFile=FindFirstFile(filename,&m_data)
if(hFile==INVALID_HANDLE_VALUE) //file not found
Make sure you close the handle if the file is found.
FindClose(hFile);
2.
You can use SHGetFileInfo()
The prototype of the function is as follows:
DWORD_PTR SHGetFileInfo(
LPCTSTR pszPath,
DWORD dwFileAttributes,
SHFILEINFO *psfi,
UINT cbFileInfo,
UINT uFlags
);
3.
PathFileExists
4. 加一個標準c庫函數
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\n" ); ???? /* Check for write permission */ ???? if( (_access( "ACCESS.C", 2 )) != -1 ) ??????? printf( "File ACCESS.C has write permission\n" ); ? } }
posted on 2006-05-10 14:14 AlanTop 閱讀(4091) 評論(4) 編輯 收藏 引用 所屬分類: C++