查找指定文件夾下的文件

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

/**********************
_finddata_t結構體:
struct _finddata_t {
 unsigned  attrib ;
 time_t  time_create ;
 time_t  time_access ;
 time_t  time_write ;
 _fsize_t  size ;
 char name [260] ;
}
**********************/

void main()
{
    _finddata_t file;
    long lf;
    if((lf = _findfirst("C://WINDOWS//*.INI",&file))==-1l)//_findfirst返回的是long型;long __cdecl _findfirst(const char *, struct _finddata_t *)
        cout<<"文件沒有找到!/n";
    else
    {
        cout<<"/n文件列表:/n";
  do {
            cout<<file.name;
            if(file.attrib == _A_NORMAL)cout<<"  普通文件  ";
            else if(file.attrib == _A_RDONLY)cout<<"  只讀文件  ";
            else if(file.attrib == _A_HIDDEN )cout<<"  隱藏文件  ";
            else if(file.attrib == _A_SYSTEM )cout<<"  系統文件  ";
            else if(file.attrib == _A_SUBDIR)cout<<"  子目錄  ";
            else cout<<"  存檔文件  ";
            cout<<endl;
        }while( _findnext( lf, &file ) == 0 );
    }
 _findclose(lf);
}

/************************************************************************
  int findfirst(char   *pathname,struct   ffblk   *ffblk,int   attrib)    
  查找指定的文件,成功返回0.pathname爲指定的目錄名和文件名,如"C://WPS//TXT",ffblk爲指定的保存文件信息的一個結構,定義如下:    
      struct   ffblk     {      
            char   ff_reserved[21];   /*DOS保留字    
            char   ff_attrib;               /*文件屬性      
            int     ff_ftime;                 /*文件時間     
            int     ff_fdate;                 /*文件日期       
  long   ff_fsize;                 /*文件長度    
  char   ff_name[13];           /*文件名         
          }                                                                        
            attrib爲文件屬性,由以下字符代表    
          FA_RDONLY   只讀文件               FA_LABEL     卷標號    
          FA_HIDDEN   隱藏文件                 FA_DIREC     目錄        
          FA_SYSTEM   系統文件                 FA_ARCH       檔案        
          例:    
          struct   ffblk   ff;    
          findfirst("*.wps",&ff,FA_RDONLY);    
   
  int findnext(struct   ffblk   *ffblk) 取匹配finddirst的文件,成功返回0   

發佈了35 篇原創文章 · 獲贊 3 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章