我正在尝试浏览一个目录,当遇到一个文本文件时,我想打开它。出于某种原因,在我点击文本文件之前,我在 while 循环上得到了一个空条件,所以它提前退出,我不知道为什么。
循环条件
while ((pDirent = readdir(pDir)) != NULL)
{
printf("*******");
printf("The filePath is: %s \n", filePath);
printf("dirent is %s \n", pDirent->d_name);
if (strcmp(pDirent->d_name, ".") == 0 || strcmp(pDirent->d_name, "..") == 0)
{
printf("get that shit %s\n", pDirent->d_name);
continue;
}
strcat(filePath, pDirent->d_name);
printf ("[%s]\n", filePath);
printf("Is regular? %d \n", IsRegularFile(filePath));
printf("Is dir? %d \n", isDirectory(filePath));
strcat(filePath, "/");
printf("%c %c %c \n",filePath[strlen(filePath) - 4], filePath[strlen(filePath) - 3],filePath[strlen(filePath) - 2]);
if (filePath[strlen(filePath) - 4] == 't' && filePath[strlen(filePath) - 3] == 'x' && filePath[strlen(filePath) - 2] == 't') //add check for length and .txt
{
printf("File is txt\n");
break;
}
printf("end of loop\n");
}
满足然后退出。我有文件结构:
测试目录(测试目录1)
测试目录1(测试目录1-2)
testDir1-2(testFile.txt)
基本上它到达 testDir1-2 然后退出循环,我的文件路径是 testDir/testDir1/testDir1-2 并且由于某种原因它永远不会得到 testFile.txt。pDirent->d_name 也为空。