2010年10月8日 星期五

[C 範例代碼] 文件操作 : 搜尋查找文件 (使用 FindFirstFileA 函數)


前言 :
編成實現文件的查找, 要求輸入搜尋路徑, 若該路徑下有符合搜尋結果則列印出來第一個找到的檔案名, 否則提示沒有找到.

技術要點 :
本範例使用了FindFirstFileA 函數, 具體說明如下 :
- 語法
  1. FindFirstFileA(  
  2.     __in  LPCSTR lpFileName,  
  3.     __out LPWIN32_FIND_DATAA lpFindFileData  
  4.     );  
- 參數
lpFileName [in]
The directory or path, and the file name, which can include wildcard characters, for example, an asterisk (*) or a question mark (?).
This parameter should not be NULL, an invalid string (for example, an empty string or a string that is missing the terminating null character), or end in a trailing backslash (\).
If the string ends with a wildcard, period (.), or directory name, the user must have access permissions to the root and all subdirectories on the path.
In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 widecharacters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see Naming a File.

lpFindFileData [out]
A pointer to the WIN32_FIND_DATAA structure that receives information about a found file or directory.

- 返回值
If the function succeeds, the return value is a search handle used in a subsequent call to FindNextFile or FindClose, and the lpFindFileData parameter contains information about the first file or directory found.
If the function fails or fails to locate files from the search string in the lpFileName parameter, the return value is INVALID_HANDLE_VALUE and the contents of lpFindFileData are indeterminate. To get extended error information, call the GetLastError function.
If the function fails because no matching files can be found, the GetLastError function returns ERROR_FILE_NOT_FOUND

- Remark
The FindFirstFile function opens a search handle and returns information about the first file that the file system finds with a name that matches the specified pattern. This may or may not be the first file or directory that appears in a directory-listing application (such as the dir command) when given the same file name string pattern. This is because FindFirstFile does no sorting of the search results. For additional information, see FindNextFile.

範例代碼 :
  1. #include   
  2. #include   
  3.   
  4. void main() {  
  5.     WIN32_FIND_DATAA FindFileData;  
  6.     HANDLE hFind;  
  7.     char sfile[100];  
  8.     printf("Enter the file search path: ");  
  9.     scanf("%s", sfile);  
  10.     hFind = FindFirstFileA(sfile, &FindFileData);  
  11.     if (hFind == INVALID_HANDLE_VALUE)   
  12.         {  
  13.         printf ("FindFirstFile failed (%d)\n", GetLastError());  
  14.         return;  
  15.     }  
  16.     else  
  17.     {  
  18.         printf("The first file found is %s\n", FindFileData.cFileName);  
  19.         FindClose(hFind);  
  20.     }  
  21. }  
執行結果 :
Enter the file search path: E:/Temp/*.c <輸入查詢字串>
The first file found is a.c

補充說明 :
API:FindClose
API:FindFirstFileEx
API:FindFirstFileTransacted
API:FindNextFile
API:GetFileAttributes
API:SetFileAttributes
This message was edited 8 times. Last update was at 05/08/2010 11:39:33

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...