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;
}
#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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询