調(diào)用的數(shù)據(jù)結(jié)構(gòu)和函數(shù):
- struct _finddata_t結(jié)構(gòu)體
- long _findfirst( char *filespec, struct _finddata_t *fileinfo )
- int _findnext( long handle, struct _finddata_t *fileinfo )
具體參數(shù)說(shuō)明可以百度盒至,或者看這篇博客
https://blog.csdn.net/yang332233/article/details/53081785
這是我剛學(xué)C++那段時(shí)間寫(xiě)來(lái)練手的呛凶,代碼含金量一般虏辫,大佬輕噴
#include<iostream>
#include<string>
#include<io.h>
#include<windows.h>
#include<fstream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int getfile(string path,int n)//遞歸函數(shù)
{
long hand;//
int s = 0;
string p = path + "\\*";//構(gòu)造查詢(xún)路徑 * 是通配符
string temp;
struct _finddata_t fileinfo;//定義結(jié)構(gòu)體
hand = _findfirst(p.c_str(), &fileinfo);獲取句柄
if (hand == -1)//如果目錄不存在直接退出查找
{
//cout<<"File cannot be found"<<endl;
return 0;
}
do
{
if (strcmp(fileinfo.name, "..") == 0 || strcmp(fileinfo.name, ".") == 0)//跳過(guò)這兩個(gè)目錄,否者會(huì)出現(xiàn)目錄混亂
continue;
if (fileinfo.attrib == _A_SUBDIR)//判斷是否為目錄
{
p = path + "\\" + fileinfo.name;
getfile(p.c_str(),n+1);//如果是目錄,構(gòu)造查詢(xún)path驹碍,執(zhí)行g(shù)etfile
}
//Sleep(100);
else//如果是文件陵吸,將路徑寫(xiě)入txt文件
{
fstream files;
files.open("E:\\TEXT.txt", ios::out | ios::app);
cout << fileinfo.name << endl;
files << path + "\\" + fileinfo.name <<endl;
files.close();
}
} while (_findnext(hand, &fileinfo) == 0);
return 0;
}
int main()
{
string filepath;
char flag;
cout << "請(qǐng)輸入路徑:" << endl;
cin >> filepath;
getfile(filepath,0);
fflush(stdin);
cout << "刪除TEXT文件按d"<<endl;
flag = getchar();
if (flag == 'd')
if (remove("E:\\TEXT.txt") == 0) cout << "mission success" << endl;
system("pause");
return 0;
}