findfirst, findnext是一個函式,用於搜尋磁碟目錄; 取得下一個匹配的findfirst模式的檔案
基本介紹
- 中文名:findfirst, findnext
- 函式名:findfirst, findnext
- 功 能:搜尋磁碟目錄; 取得下
- 用 法:int findfirst(char *pathn
函式名
功 能
用 法
程式例1
/* findnext example */#include <stdio.h>#include <dir.h>//兩個函式需要定義一個結構體來存儲函式返回的數據。結構體如下:struct ffblk{char ff_reserved[21]; /*DOS保留字*/char ff_attrib; /*檔案屬性*/int ff_ftime; /*檔案時間*/int ff_fdate; /*檔案日期*/long ff_fsize; /*檔案長度*/char ff_name[13]; /*檔案名稱*/}//將結構體中的ff_name[13]顯示出來即可。int main(void){struct ffblk ffblk;int done;printf("Directory listing of *.*\n");done = findfirst("*.*",&ffblk,0);while (!done){printf(" %s\n", ffblk.ff_name);done = findnext(&ffblk);}return 0;}
程式例2
#include <io.h>#include <iostream>#include <string>#include <windows.h>using namespace std;string sRoot = "D:\\";string sSuffix = "\\*.*"; // 後綴void Move(string sPath);void main(){Move(sRoot);system("pause");}void Move(string sPath){struct _finddata_t file;long hFile, hFileNext;string sPathLast = sPath + sSuffix; // sPathLast = "c:\test\*.*"hFile = _findfirst(sPathLast.c_str(), &file);if(hFile == -1){cout<<"檔案不存在."<<endl;return;}else{cout<<file . name<<endl;}hFileNext = _findnext(hFile, &file);while(_findnext(hFile, &file) == 0){if(file.attrib == _A_SUBDIR){string sAddPath = sPath;sAddPath += "\\";sAddPath += file . name;cout<<"目錄:"<<sAddPath<<endl;Move(sAddPath);}else{string sAddPath = sPath;sAddPath += "\\";sAddPath += file . name;cout<<"檔案:"<<sAddPath<<endl;}}}