c - 使用 _finddata_t->name 浏览子目录

标签 c visual-c++ if-statement struct subdirectory

我使用适用于 Windows XP 的 Visual Studio 2010。这个函数用于打印所有子目录,但我面临着可怕的问题。请参阅以下代码:

typedef struct _finddata_t FILE_SEARCH;

void fileListPrint(char* path, FILE_SEARCH* file_search, int deep){ 
intptr_t h_file;
int i, isForD;
char cwd[256];
char* temp = path;  
FILE_SEARCH fileSearch;

temp = (char*)malloc(256);

for(i=0; i<strlen((path)); i++)
{
    temp[i] = path[i];
}
temp[i-3] = '\0';


if((h_file = _findfirst(path, file_search)) == -1L)
    errorHandling("doesn't exist file\n");


printf("[%s] 출력 [%d] 깊이 \n", path, deep);
do
{       
    printf("%s\n", file_search->name);              
    isForD = isFileOrDir(strncat(temp, file_search->name,sizeof(temp) + sizeof(file_search->name)));        
    printf("%d\n", isForD);

    temp[i-3] = '\0';       

    if(file_search->name == ".")
        printf("same\n");
    else
        printf("diff\n");

    if(0 == isForD)
    {                   
        strncat(temp, "\\*.*", sizeof(temp) + 5);
        Sleep(1000);
        fileListPrint(temp, &fileSearch, deep+1);                       
        temp[i-3] = '\0';       }
    else if(1 == isForD)
    {
        temp[i-3] = '\0';       
        continue;
    }
    else if(file_search->name == ".")
    {
        temp[i-3] = '\0';       
        break;
    }



}while(_findnext(h_file, file_search) == 0);

_findclose(h_file);

free(temp);
}

我试图理解这段代码片段:

 if(file_search->name == ".")
        printf("same\n");
    else
        printf("diff\n");

为什么不一样?

打印打印结果:

[D:\*.*] 출력 [0] 깊이
.
0
diff
[D:\1\*.*] 출력 [1] 깊이
.
0
diff
[D:\1\.\*.*] 출력 [2] 깊이
.
0
diff
[D:\1\.\.\*.*] 출력 [3] 깊이
.
0
diff

无限循环。

我应该如何解决这个问题?

最佳答案

if(file_search->name == ".") 

我的 C 版本不允许使用 == 进行字符串比较。也许您想要 strcmp() ?

关于c - 使用 _finddata_t->name 浏览子目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18731293/

相关文章:

c - 在有或没有 UNICODE 支持的情况下,如何在我的程序中使用 _stprintf?

c - 浮点运算的精度

visual-studio - VC++ Visual Studio 在子目录中添加了 .hpp 文件,但得到 "Error: cannot open source file ..."

javascript - jQuery\Javascript - if 语句无法正确计算

c - 将字符串放在 if/else 括号内?

c - c中的strcat模拟

c - 多个 xterm 伪终端仅用作多个线程的输出

visual-c++ - 如何在 Visual Studio 2008 C++ 中链接 .lib

c++ - 无法将参数 5 从 'SIZE_T *' 转换为 'size_t *'——为什么?

python - 追加函数嵌套在 IF 语句主体中不起作用