none
为何会搜到这样的目录 RRS feed

  • 问题

  • 编一个小程序实现搜索指定目录下的文件夹,代码如下:

          
    #include "stdafx.h"
    
    #include <windows.h>
    #include <strsafe.h>
    
    #include <tchar.h>
    #include <stdio.h>
    #include <conio.h>
    
    #define BUFSIZE 4096
    
    static void ErrorHandler(LPTSTR lpszFunction) 
    { 
    	// Retrieve the system error message for the last-error code
    
    	LPVOID lpMsgBuf;
    	LPVOID lpDisplayBuf;
    	DWORD dw = GetLastError(); 
    
    	FormatMessage(
    		FORMAT_MESSAGE_ALLOCATE_BUFFER | 
    		FORMAT_MESSAGE_FROM_SYSTEM |
    		FORMAT_MESSAGE_IGNORE_INSERTS,
    		NULL,
    		dw,
    		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    		(LPTSTR) &lpMsgBuf,
    		0, NULL );
    
    	// Display the error message and exit the process
    
    	lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
    		(lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR)); 
    	StringCchPrintf((LPTSTR)lpDisplayBuf, 
    		LocalSize(lpDisplayBuf) / sizeof(TCHAR),
    		TEXT("%s failed with error %d: %s"), 
    		lpszFunction, dw, lpMsgBuf); 
    	MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); 
    
    	LocalFree(lpMsgBuf);
    	LocalFree(lpDisplayBuf);
    }
    
    void _tmain(int argc, TCHAR *argv[])
    {
    	WIN32_FIND_DATA ffd;
    	LARGE_INTEGER filesize;
    	TCHAR szDir[MAX_PATH] = _T("D:\\OSG Src Code\\include");
    	size_t length_of_arg;
    	HANDLE hFind = INVALID_HANDLE_VALUE;
    	DWORD dwError=0;
    
    	StringCchCat(szDir, MAX_PATH, TEXT("\\*"));
    
    	// Find the first file in the directory.
    
    	hFind = FindFirstFile(szDir, &ffd);
    
    	if (INVALID_HANDLE_VALUE == hFind) 
    	{
    		ErrorHandler(TEXT("FindFirstFile"));
    		return;
    	} 
    
    	// List all the files in the directory with some info about them.
    	do
    	{
    		// if the file is directory and not hidden,print it
    		if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    			&&((ffd.dwFileAttributes &FILE_ATTRIBUTE_HIDDEN)==0))
    		{
    			_tprintf(TEXT(" %s  <DIR>\n"), ffd.cFileName);
    		}
    		else
    		{
    			// if the file is normal file
    			filesize.LowPart = ffd.nFileSizeLow;
    			filesize.HighPart = ffd.nFileSizeHigh;
    			_tprintf(TEXT(" %s  %ld bytes\n"), ffd.cFileName, filesize.QuadPart);
    		}
    	}
    	while (FindNextFile(hFind, &ffd) != 0);
    
    	dwError = GetLastError();
    	if (dwError != ERROR_NO_MORE_FILES) 
    	{
    		ErrorHandler(TEXT("FindFirstFile"));
    	}
    
    	FindClose(hFind);
    	getch();
    }
    
    

       运行结果如下:
       

         如上图,我很奇怪为何会搜到.和.. 这样的目录,而且它们居然还不是FILE_ATTRIBUTE_HIDDEN和FILE_ATTRIBUTE_SYSTEM属性的。请问这两个目录的作用是什么呢?

     

     


    前无古人,后无来者
    2011年4月10日 5:13

答案

  • . 表示当前目录,
    .. 表示上一级目录
    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
    2011年4月10日 10:33
    版主