#include <stdio.h> #include <stdlib.h> #include <string> #include <windows.h> using namespace std;
void print_table(int tableCount, int tableSize)
{
<span style="white-space:pre"> </span>if(tableCount <= 0)
<span style="white-space:pre"> </span>return;
<span style="white-space:pre"> </span>char *p = (char*)malloc( (tableSize*(tableCount) + 1)*sizeof(char) );
<span style="white-space:pre"> </span>int i = 0;
<span style="white-space:pre"> </span>for(i=0; i<tableCount*tableSize; i++)
<span style="white-space:pre"> </span>p[i] = ' ';
<span style="white-space:pre"> </span>p[i] = '\0';
<span style="white-space:pre"> </span>printf(p);
<span style="white-space:pre"> </span>free(p);
}
int GetFileList(string strDir, int tableCount)//strDir:目录地址; tableCount:退格符数目,初始值为0
{
WIN32_FIND_DATA find_data;
HANDLE hFind = INVALID_HANDLE_VALUE;
string strSearch;
strSearch = strDir + "\\*";//添加通配符
hFind = FindFirstFile(strSearch.c_str(), &find_data);
do
{
if(find_data.cFileName[0] != '.')
{
print_table(tableCount, 4);
printf("%s\n", find_data.cFileName);
if(find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
GetFileList(strDir + "\\" + find_data.cFileName, tableCount + 1);
}
}
}
while( FindNextFile(hFind, &find_data) != 0 );
FindClose(hFind);
return 0;
}int main()
{
GetFileList("C:\\Users\\Paladin\\Desktop", 0);
return 0;
}原文地址:http://blog.csdn.net/jinshandayouxia/article/details/43793417