您的位置:首页 > 其它

获取目录的的所有文件,并给出列表

2015-02-13 13:27 162 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: