C语言问题 已知某一路径,如何得到该路径下的某一文件夹的路径?

如题所述

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <io.h>

#define MAX_FILE_PATH_LEN 128
#define INVALID_HANDLE -1

int main()
{
char aucFilePath[MAX_FILE_PATH_LEN + 1] = {0};
char aucTempPath[MAX_FILE_PATH_LEN + 1] = {0};
char *pFilePath = NULL;
int iDirNum = 0;
int iFileNum = 0;

_finddata_t finddata = {0};
long findhandle = INVALID_HANDLE;

//获取当前目录,也可以做入参传入aucFilePath
pFilePath = getcwd(aucFilePath, MAX_FILE_PATH_LEN);
if (NULL == pFilePath)
{
printf("get current working directory fail, \n"
"errno = %d, errro description : %s\n", errno, strerror(errno));
getchar();
return -1;
}

printf("current working directory = %s\n", aucFilePath);
strncpy(aucTempPath, aucFilePath, strlen(aucFilePath));
strncat(aucTempPath, "\\*", MAX_FILE_PATH_LEN);

findhandle = _findfirst(aucTempPath, &finddata);
if(findhandle != INVALID_HANDLE)
{
do {
if(finddata.attrib & _A_SUBDIR)
{
if ((0 != strcmp(finddata.name, ".")) && (0 != strcmp(finddata.name, "..")))
{
//子目录个数
iDirNum++;
printf("sub directory name = %s, path = %s\\%s, iDirNum = %d\n",
finddata.name, aucFilePath, finddata.name, iDirNum);
}
}
else
{
//子文件个数
iFileNum++;
printf("file = %s, iFileNum = %d\n", finddata.name, iFileNum);
}
}while(0 == _findnext(findhandle, &finddata));
_findclose(findhandle);
}

getchar();
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-04-09
有函数,findfirst, findnext
第2个回答  2016-04-09
cmd
相似回答