【问题标题】:problems with searching in files and directories .. windows programming在文件和目录中搜索的问题.. windows 编程
【发布时间】:2010-04-23 17:44:38
【问题描述】:

我正在研究这本书(Addison Wesley Windows System Programming 第 4 版),我认为它没用) 没有语法错误,但输出不是我想要的搜索输出是这样的:

    not found
Now, here are the folders:
not found
Searching in d:\iust\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.
\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.
\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.
\.\.\.\.\.\..\e-books\.\.\.\.\E-BOOKS
The file name is: d:\iust\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\
.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\
.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\
.\.\.\.\.\.\.\.\..\e-books\.\.\.\.\E-BOOKS\*Test*
not found
Now, here are the folders:
not found
Searching in d:\iust\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.
\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.
\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.
\.\.\.\.\.\..\e-books\.\.\.\..
The file name is: d:\iust\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\
.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\
.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\
.\.\.\.\.\.\.\.\..\e-books\.\.\.\..\*Test*
not found
Now, here are the folders:

首先我注意到无论我做什么它都不会只在我指定的文件夹内搜索,而是在整个驱动器中搜索,第二个烦人的问题是 DOTS .和..那些出现在每个文件夹中我怎样才能避免这个问题。现在正如我之前所说的,我使用的是我之前提到的书,但我不知道我只是不喜欢我所做的,有没有更好的方法来形成我的代码。

代码:

#include "stdafx.h"
#include <windows.h>

void SearchForFile(TCHAR *folder, TCHAR *file){
    _tprintf(L"Searching in %s\n",folder); //just to show the state
    TCHAR temp[1000];

    _stprintf(temp,L"%s\\%s",folder,file); // here  wrote into temp the location as folder/file
    _tprintf(L"The file name is: %s\n",temp);
    HANDLE f;
    WIN32_FIND_DATA data;
    f=FindFirstFile(temp,&data);
    if(f==INVALID_HANDLE_VALUE){
        _tprintf(L"not found\n");

    }
    else{
        _tprintf(L"found this file: %s\n",data.cFileName);
        while(FindNextFile(f,&data)){
            _tprintf(L"found this file: %s\n",data.cFileName);
        }
        FindClose(f);   
    }

    _stprintf(temp,L"%s\\*",folder); // "d:\*" for example
    _tprintf(L"Now, here are the folders:\n");
    f=FindFirstFile(temp,&data);
    TCHAR temp2[1000];
    if(f==INVALID_HANDLE_VALUE){
        _tprintf(L"not found\n");

    }
    else{
        if((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
            {

            //_tprintf(L"found this directory: %s\n",data.cFileName);
                _stprintf(temp2,L"%s\\%s",folder,data.cFileName);
                SearchForFile(temp2,file);
            }
        while(FindNextFile(f,&data)){//         _tprintf(L"%d   %d\n",data.dwFileAttributes,FILE_ATTRIBUTE_DIRECTORY);
            if((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
            //  _tprintf(L"found this directory: %s\n",data.cFileName);
            {
                _stprintf(temp2,L"%s\\%s",folder,data.cFileName);
                SearchForFile(temp2,file);

            }
        }
        FindClose(f);   
    }
}


int _tmain(int argc, _TCHAR* argv[])
{
    SearchForFile(L"d:\\test", L"*Test*");
    return 0;
}

【问题讨论】:

    标签: c winapi


    【解决方案1】:

    您必须过滤掉每个文件夹中的 ... 伪文件夹。
    粗略地说,在您的递归分支中:

    if((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 
         && data.data.cFileName != "." 
         && data.data.cFileName != "..")
    

    【讨论】:

    • 哦,我明白了thanx,,但是你认为有办法改进它的代码我自己不相信(我的第一次)还是你认为它很好?有没有办法让递归更好
    • 您的代码基本上没问题,对于生产代码,我会添加更多错误检查。例如在 wile-loops 中(如果有人在您读取 U 盘的文件夹时拔出 U 盘怎么办?)
    • 好消息是它没有退出特定文件夹,但仍在查看伪文件夹,并且在 sec 之后,控制台将给出一个关闭错误我添加了以下代码,因为你添加了 if(( data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 && data.cFileName != L"." && data.cFileName != L"..") 对吗?!!
    • 我是这样做的 if((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)!=0 && data.cFileName[0] != L'.' )
    • @krstian,拥有.Test 文件夹是完全合法的。你会跳过那个。不要过早优化。
    【解决方案2】:

    一般来说,您应该跳过“.”。和“..”目录,它们是“当前”和“父”目录的同义词。

    【讨论】:

      【解决方案3】:

      无论您如何在 Windows 上找到目录的内容,第一个匹配项都是“.”。 (当前目录)和“..”(父目录)。您可能想忽略它们。

      【讨论】:

        【解决方案4】:

        通常您会显式测试并跳过“。”和“..”存在于所有目录(但根目录)中的子目录。您正在使用的代码递归搜索子目录,并且由于您没有忽略“..”目录,因此它将搜索该目录,最终将导致根目录,并从那里搜索所有子目录 - 意思是它'将搜索整个磁盘。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-29
          • 1970-01-01
          • 2010-11-19
          • 1970-01-01
          相关资源
          最近更新 更多