请问怎样遍历同一目录下的所有相同类型的文件?(不用win-API更好)?

gully 2002-03-17 12:14:07
请问怎样遍历同一目录下的所有相同类型的文件?(不用win-API更好)?
比如目录c:\web\下的所有.htm 文件
...全文
72 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
gully 2002-03-17
  • 打赏
  • 举报
回复
o 就是有一点难读. ^_^ 功能强大 牺牲了可读性哦. :(
aqua_aqua 2002-03-17
  • 打赏
  • 举报
回复
对了,上面的代码是用纯C写的,应该你可以用到。
aqua_aqua 2002-03-17
  • 打赏
  • 举报
回复
if(((fblk.attrib&_A_SUBDIR)!=0)&&strcmp(fblk.name,".")&&strcmp
(fblk.name,".."))
fblk.attrib&_A_SUBDIR 是代表这个目录下的子目录,
同时在每个目录下都有两个系统自动生成的文件。这两个文件的文件名就是"."和"..",所以要去掉。
唉呀,其这个条件语句没有也行了。我这是从原来的文件中截下来的。
gully 2002-03-17
  • 打赏
  • 举报
回复
to : aqua_aqua(丁丁)
了解了,谢谢 ! :)
gully 2002-03-17
  • 打赏
  • 举报
回复
to vickowang(你的影子无所不在...)
谢谢! :)
你的代码非常好,但CFileFind类是MFC的啊
能有更底层的吗?
aqua_aqua 2002-03-17
  • 打赏
  • 举报
回复
windows的文件信息不是统一保存在一起的,就像我上面给你的代码,每个文件的信息保存在 _finddata_t这样的一个结构中,而这个结构是各文件在一起的,不过它是在文件的最前部,在这之后的部分才是文件的内容。
gully 2002-03-17
  • 打赏
  • 举报
回复
if(((fblk.attrib&_A_SUBDIR)!=0)&&strcmp(fblk.name,".")&&strcmp
(fblk.name,".."))
//这句不好理解哦,可以写清楚一点么?谢了先 :)
vickowang 2002-03-17
  • 打赏
  • 举报
回复
可以用CFileFind类的成员函数。
例如:
strPath = "c:\web\";
strExtMask = "*.htm";
void GetFileList(CString strPath, CString strExtMask)
{
CFileFind finder;
CString strNewPth;//可以得到你想要的文件的路径
BOOL bWorking = finder.FindFile(strPath + strExtMask);

while (bWorking)
{
bWorking = finder.FindNextFile();
//忽略 .和 ..文件; 否则就
//死循环了!
if (finder.IsDots())
continue;
//如果是目录,递归搜索
if (finder.IsDirectory())
{
strNewPath = finder.GetFilePath();
strNewPath += "\\";
strNewPath += strExtMask;
}
else
{
strNewPath = finder.GetFilePath();
}
}
finder.Close();
}
gully 2002-03-17
  • 打赏
  • 举报
回复
在linux和unix中文件的信息是保存在它的目录文件中的,Windows中呢?
gully 2002-03-17
  • 打赏
  • 举报
回复
我对Windows的文件系统不太熟悉,望赐教!
aqua_aqua 2002-03-17
  • 打赏
  • 举报
回复
对了你还可以试试下面这个方法。
_finddata_t fblk;
long handle,result;
char child[_MAX_PATH];
child = "c:\\web\\*.htm";
result = handle = _findfirst(child,&fblk);

while(result !=-1)
{
if(((fblk.attrib&_A_SUBDIR)!=0)&&strcmp(fblk.name,".")&&strcmp
(fblk.name,".."))
{你想于什么就在这干吧。
}
result = _findnext(handle,&fblk);
}
_findlclose(handle);
}
gully 2002-03-17
  • 打赏
  • 举报
回复
我的意思是用更加接近机器的方法!重要的是对文件系统的理解!
flywolfman 2002-03-17
  • 打赏
  • 举报
回复
9494 同意楼上的。
aqua_aqua 2002-03-17
  • 打赏
  • 举报
回复
为什么不用API?
API中的FindFirstFile(),和FindNextFile()不是挺好用的吗?
gully 2002-03-17
  • 打赏
  • 举报
回复
怎样联系啊?
aqua_aqua 2002-03-17
  • 打赏
  • 举报
回复
互相学习吗!
gully 2002-03-17
  • 打赏
  • 举报
回复
呵呵
以后要多多请教你了
aqua_aqua 2002-03-17
  • 打赏
  • 举报
回复
这到没事,我也经常受得到别人的帮助。这就是互相帮助吗?
gully 2002-03-17
  • 打赏
  • 举报
回复
^_^
谢了! 给分
aqua_aqua 2002-03-17
  • 打赏
  • 举报
回复
_findclose
Closes the specified search handle and releases associated resources.

int _findclose( long handle );

Function Required Header Compatibility
_findclose <io.h> Win 95, Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

If successful, _findclose returns 0. Otherwise, it returns –1 and sets errno to ENOENT, indicating that no more matching files could be found.

Parameter

handle

Search handle returned by a previous call to _findfirst

System Calls Routines | _find, _wfind Function Overview


--------------------------------------------------------------------------------
Send feedback to MSDN.Look here for MSDN Online resources.
加载更多回复(6)

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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