#include <iostream>
#include <cstring> // for strcpy(), strcat()
#include <io.h>
using namespace std;
void listFiles(const char * dir);
int main()
{
char dir[200];
cout << "Enter a directory: ";
cin.getline(dir, 200);
listFiles(dir);
</span><span style="color: #0000ff">return</span> <span style="color: #800080">0</span><span style="color: #000000">;
}
void listFiles(const char dir)
{
char dirNew[200];
strcpy(dirNew, dir);
strcat(dirNew, "."); // 在目录后面加上".*"进行第一次搜索
intptr_t handle;
_finddata_t findData;
handle </span>= _findfirst(dirNew, &<span style="color: #000000">findData);
</span><span style="color: #0000ff">if</span> (handle == -<span style="color: #800080">1</span>) <span style="color: #008000">//</span><span style="color: #008000"> 检查是否成功</span>
<span style="color: #0000ff">return</span><span style="color: #000000">;
</span><span style="color: #0000ff">do</span><span style="color: #000000">
{
</span><span style="color: #0000ff">if</span> (findData.attrib &<span style="color: #000000"> _A_SUBDIR)
{
</span><span style="color: #0000ff">if</span> (strcmp(findData.name, <span style="color: #800000">"</span><span style="color: #800000">.</span><span style="color: #800000">"</span>) == <span style="color: #800080">0</span> || strcmp(findData.name, <span style="color: #800000">"</span><span style="color: #800000">..</span><span style="color: #800000">"</span>) == <span style="color: #800080">0</span><span style="color: #000000">)
</span><span style="color: #0000ff">continue</span><span style="color: #000000">;
cout </span><< findData.name << <span style="color: #800000">"</span><span style="color: #800000"> <dir>
</span><span style="color: #800000">"</span><span style="color: #000000">;
</span><span style="color: #008000">//</span><span style="color: #008000"> 在目录后面加上"\"和搜索到的目录名进行下一次搜索</span>
strcpy(dirNew, dir);
strcat(dirNew, "");
strcat(dirNew, findData.name);
listFiles(dirNew);
}
</span><span style="color: #0000ff">else</span><span style="color: #000000">
cout </span><< findData.name << <span style="color: #800000">"</span><span style="color: #800000"> </span><span style="color: #800000">"</span> << findData.size << <span style="color: #800000">"</span><span style="color: #800000"> bytes.
</span><span style="color: #800000">"</span><span style="color: #000000">;
} </span><span style="color: #0000ff">while</span> (_findnext(handle, &findData) == <span style="color: #800080">0</span><span style="color: #000000">);
_findclose(handle); </span><span style="color: #008000">//</span><span style="color: #008000"> 关闭搜索句柄</span>
}