`

用 _findfirst 和 _findnext 查找文件,遍历文件夹

    博客分类:
  • c++
阅读更多
标签: _findfirst _findnext 杂谈 分类: C
一、这两个函数均在io.h里面。

二、首先了解一下一个文件结构体:
struct _finddata_t {
    unsigned    attrib;
    time_t      time_create;  
    time_t      time_access;  
    time_t      time_write;
    _fsize_t    size;
    char        name[260];
};

time_t,其实就是long
而_fsize_t,就是unsigned long

现在来解释一下结构体的数据成员吧。

attrib,就是所查找文件的属性:_A_ARCH(存档)、_A_HIDDEN(隐藏)、_A_NORMAL(正常)、_A_RDONLY(只读)、 _A_SUBDIR(文件夹)、_A_SYSTEM(系统)。

time_create、time_access和time_write分别是创建文件的时间、最后一次访问文件的时间和文件最后被修改的时间。

size:文件大小

name:文件名。


三、用 _findfirst 和 _findnext 查找文件

1、_findfirst函数:long _findfirst(const char *, struct _finddata_t *);

第一个参数为文件名,可以用"*.*"来查找所有文件,也可以用"*.cpp"来查找.cpp文件。第二个参数是_finddata_t结构体指针。若查找成功,返回文件句柄,若失败,返回-1。


2、_findnext函数:int _findnext(long, struct _finddata_t *);

第一个参数为文件句柄,第二个参数同样为_finddata_t结构体指针。若查找成功,返回0,失败返回-1。

3、_findclose()函数:int _findclose(long);

只有一个参数,文件句柄。若关闭成功返回0,失败返回-1。


四、查找文件例子

#include <io.h>
#include <iostream>
using namespace std;

int main()
{
_finddata_t file;
int k;
long HANDLE;
k = HANDLE = _findfirst( "*.*", &file );
while( k != -1 )
{
  cout << file.name << endl;
  k = _findnext( HANDLE, &file );
}
_findclose( HANDLE );

return 0;
}

转自:http://blog.sina.com.cn/s/blog_67e046d10100jwdo.html
分享到:
评论

相关推荐

    C++利用 _findfirst与_findnext查找文件的方法

    _findfirst与_findnext查找文件 一、这两个函数均在io.h里面。 二、首先了解一下一个文件结构体: struct _finddata_t { unsigned attrib; time_t time_create; time_t time_access; time_t time_wr

    delphi_遍历文件夹

    while SRI = 0 do begin if (SR.Name = '.') or (SR.Name = '..') then begin //不用做 end else if SR.Attr = faDirectory then begin if CheckBox1.Checked then ... SRI := FindNext(SR); end;

    delphi遍历文件夹的例子

    FindFirst 、FindNext很简单的,适合新手

    c 更改文件修改日期

    C语言实现,遍历一个文件夹,并且将修改该文件夹所有文件的修改日期为最近2月。 只有一个文件,在VC下编译通过。...调用_findfirst、_findnext,纯C递归实现 大家只要修改里面的changeFiletime函数就可以自己定制功能。

    Delphi文件查找示例

    Delphi操作文件的一个简单函数示例,主要是用到Win31中的DriveComboBox和DirectoryListBox两个组件及FindFirst,FindNext和FindClose三个文件操作函数。 ///////////////////////////////////////////////// ...

    Delphi 遍历windows目录下的文件

    Delphi中使用System.SysUtils.findFirst,findnext和findclose函数进行目录遍历时,在Windows10及win11中会出现error code 18错误,在此提供一个解决方案,直接使用winapi完美解决

    OneDrive-xFsRedir 类似文件系统操作函数集合

    onedrive_find_open/onedrive_find_next, 类似 FindFirstFile和FindNext函数, onedrive_stat,类似GetFileInformationByHandle等获取文件属性的函数, onedrive_mkdir, 类似CreateDirectory创建目录函数 onedrive_...

    Qt5开发及实例-CH204.rar,Qt5在QMap中的插入、遍历和修改的代码

    实例CH204,在QMap中的插入、遍历和修改。 (a) for(;i.hasNext();)、qDebug()()().value():完成对QMap的遍历输出。...因此,在本例中通过查找值的函数QMutableMapIterator,T&gt;::findNext()来实现查找和修改。

    excel vba findnext

    excel vba 使用findext实现快速查找

    Qt5开发及实例-CH202.rar,Qt5实现QList读写遍历方法的代码

    实例CH202,通过控制台程序实现QList读写遍历方法。 (a) i.insert(j):通过QMutableListIterator&lt;T&gt;::insert()插入操作...对于findNext()和findPrevious()有些特殊:当findNext()(或findPrevious())查找到列表项的时候

    公司用的trace记录

    intptr_t hand = _findfirst( "E:\\source\\repos\\trace\\Debug\\*.tlog", &fileinfo; ); if( hand == -1 ) { return; } files.push_back( fileinfo.name ); while( _findnext( hand, &fileinfo; ) == 0 ) ...

    Classjseek

    一个用来查询文件的类 该类允许你使用findfirst/findnext的方法来查询你的文件系统。

    DELPHI搜索文件的示例

    if FindFirst(Path + '*', faAnyfile, sch) = 0 then begin repeat Application.ProcessMessages; if ((sch.Name = '.') or (sch.Name = '..')) then Continue; if DirectoryExists(Path + sch.Name) then ...

    C++内存查找实例

    本文实例讲述了C++内存查找的方法,分享给大家供大家参考。...BOOL FindFirst(DWORD dwValue);  BOOL FindNext(DWORD dwValue);  HANDLE g_hProcess;  DWORD g_arList[1024];  DWORD g_nListCnt;    BOOL

    DELPHI TXT 树文档 管理器

    if FindFirst(sDir + '*.*', faAnyFile, sr) = 0 then repeat if (sr.Name &lt;&gt; '.') and (sr.Name &lt;&gt; '..') then begin Result := False; break; end; until FindNext(sr) &lt;&gt; 0; FindClose(sr); end; { 是...

    显示任意一个目录的文件到Treeview中

    if FindFirst(Directory + '*.*' , faDirectory, SearchRec) = 0 then begin repeat if (SearchRec.Attr and faDirectory = faDirectory) and (SearchRec.Name[1] &lt;&gt; '.') then begin if (SearchRec.Attr...

    两个CADODatabse class and the CADORecordset class,这是为了在C++中更好地进行ADO数据库操作,同时带有示例程序

    FindFirst FindNext GetBookmark SetBookmark SetFilter SetSort GetRecordset GetActiveConnection Clone SaveAsXML OpenXML OpenSchema Requery The CADOParameter Class CADOParameter SetPrecision SetScale ...

    NSIS用户手册(中文版)

    FindFirst FindNext 卸载程序指令 WriteUninstaller 混合指令 GetErrorLevel GetInstDirError InitPluginsDir Nop SetErrorLevel SetRegView SetShellVarContext Sleep 字符串操作指令 StrCpy StrLen...

    发送程序-源码

    DBGridEh1.FieldColumns['fCarNum'].PickList.Add(Copy(Trim(Q.FieldByName('plate_num').AsString),4,mlen - 3)) //为了使用查找方便 else DBGridEh1.FieldColumns['fCarNum'].PickList.Add(Trim(Q.FieldByName...

Global site tag (gtag.js) - Google Analytics