windows下遍历文件(高手帮忙解答) 十分感谢

大柿子 2009-05-06 06:02:56
下面的代码只是 遍历出文件,并没有路径。怎么把 全路径也带上,,十分感谢
#include<stdio.h>
#include<windows.h>
void find(char * lpPath)
{
char szFind[MAX_PATH];
WIN32_FIND_DATA FindFileData;

strcpy(szFind,lpPath);
strcat(szFind,"\\*.*");

HANDLE hFind=FindFirstFile(szFind,&FindFileData);
if(INVALID_HANDLE_VALUE == hFind)
exit(0);

while(TRUE)
{
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if(FindFileData.cFileName[0]!='.')
{
strcpy(szFind,lpPath);
strcat(szFind,"\\");
strcat(szFind,FindFileData.cFileName);
find(szFind);
}
}
else
{
printf("%s\n",FindFileData.cFileName);
}
if(!FindNextFile(hFind,&FindFileData)) break;
}
FindClose(hFind);
}

void main()

{

find("d:\\jiang");

}
...全文
77 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
大柿子 2009-05-07
  • 打赏
  • 举报
回复
谢谢 上面帮助 我的 前辈~!~!~! 哈哈 这下好啦 ~!~! 谢谢
ououo 2009-05-06
  • 打赏
  • 举报
回复
不让修改了....

上面代码缺个 delete [] fullPath;

vs2005
ououo 2009-05-06
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <io.h>
#include <memory>

void Find(char *folderPath, char *filter = "*.*")
{
size_t fullPathLen = strlen(folderPath) + strlen(filter) + 1;
char *fullPath = new char[fullPathLen];
memset(fullPath, 0, fullPathLen);
strcpy_s(fullPath, fullPathLen, folderPath);
strcat_s(fullPath, fullPathLen, filter);

_finddata_t fileData;
memset(&fileData, 0, sizeof(fileData));
intptr_t hFile = _findfirst(fullPath, &fileData);
do
{
printf("%s%s%c\r\n", folderPath, fileData.name, fileData.attrib & _A_SUBDIR ? '\\' : ' ');
} while (_findnext(hFile, &fileData) == 0);
}

int main()
{
Find("F:\\downloads\\");
return 0;
}
jixingzhong 2009-05-06
  • 打赏
  • 举报
回复
void find(char * lpPath)

参数 lpPath就是路径
ch_tei_hyou 2009-05-06
  • 打赏
  • 举报
回复
lpPath +cFileName - -
Proteas 2009-05-06
  • 打赏
  • 举报
回复
char * lpPath 就是目录。拼起来。
大柿子 2009-05-06
  • 打赏
  • 举报
回复
在线等答案 。。谢谢各位

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧