c语言 获取目录文件列表

#include <stdio.h>
#include <windows.h>

char** GetFileList(const char *path, int *cnt)
{
	HANDLE hRes;
	WIN32_FIND_DATA data;
	LPCSTR IpFileName;
	int count = 0;
	char tmpDir[MAX_PATH] = "";
	char ** fileList;
	int i = 0;

	strcat(tmpDir,path);
	strcat(tmpDir,"\\*");    //匹¥配?所ù有瓺的?文?件t类え?型í
	IpFileName = tmpDir;

	hRes = FindFirstFileEx(IpFileName,FindExInfoStandard,&data,FindExSearchNameMatch,NULL,0);

	//计?算?该?目?录?下?文?件t的?个?数簓
	while(FindNextFile(hRes,&data))
	{
		if(strcmp(data.cFileName,".") ==0 || strcmp(data.cFileName,"..") == 0)
			continue;

		count++;
	}

	fileList = (char**)malloc(sizeof(char*)*count);

	hRes = FindFirstFileEx(IpFileName,FindExInfoStandard,&data,FindExSearchNameMatch,NULL,0);

	//计?算?该?目?录?下?文?件t的?个?数簓
	while(FindNextFile(hRes,&data))
	{
		if(strcmp(data.cFileName,".") ==0 || strcmp(data.cFileName,"..") == 0)
			continue;

		fileList[i] = (char*)malloc(strlen(data.cFileName)+1);
		memset(fileList[i],0,strlen(data.cFileName)+1);

		strcpy(fileList[i],data.cFileName);

		i++;
	}
	*cnt = i;
	return fileList;
}

int main()
{
	char *path = "E:/ubuntuShare";
	int res,i;
	char **fileList;

	fileList = GetFileList(path,&res);

	for(i = 0; i < res; i++)
	{
		printf("%s\n",fileList[i]);
	}

	return 0;
}

你可能感兴趣的:(C语言,列目录)