我正在学习这本书(Addison Wesley Windows系统编程第4版),我认为它没用我正在寻找支持递归的代码,因此它可以深入在文件和目录的代码工作(我猜)没有语法错误,但输出是不是我想要什么了把搜索的是这样的:在文件和目录中搜索的问题.. Windows编程
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:
首先我notiiced说什么都我也不会搜索只是在我指定的文件夹内,但在所有整个驱动器和第二恼人的探针是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;
}
哦,我看到thanx ,,,但你认为有什么方法可以改善它吗?我的自我不相信(我第一次)或者你认为它很好吗?有没有办法使递归更好 – 2010-04-23 18:00:44
你的代码基本上可以,对于生产代码我会添加更多的错误检查。例如在wile-loops里面(如果有人在读取它的文件夹的时候拉动USB棒?) – 2010-04-23 18:08:52
好消息是它没有离开特定的文件夹,但它仍然在查看伪文件夹, consoule会给添加以下代码为u加入 如果((data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)密切错误!= 0 \t \t \t && data.cFileName!= L “” \t \t \t && data.cFileName! = L“..”) 是这样吗?!! – 2010-04-23 18:47:20