listing files in a folder using C in Windows

后端 未结 3 486
走了就别回头了
走了就别回头了 2021-01-29 05:21

I want to list the files in this folder \"C:\\home\\WORK\\Desktop\\Communication\". There are ten files in this folder. My code has no error but it\'s not printing anything. Wh

3条回答
  •  死守一世寂寞
    2021-01-29 06:06

    Seems, you should move

    numberOfFiles++  
    

    past

    printf("%s\n",files[numberOfFiles]);
    

    Or

    while((FindNextFile(hFind,&fdata))!=0)
    {
        files[numberOfFiles]=fdata.cFileName;
        printf("%s\n", files[numberOfFiles++]);
    }
    

    Which is same as

    while((FindNextFile(hFind,&fdata))!=0)
    {
        printf("%s\n", files[numberOfFiles++] = fdata.cFileName);
    }
    

    I don't know WinAPI well, but I feel, that all files will contain invalid pointers after FindClose(hFind) as well as all elements of files will point to *(fdata.cFileName) which will be released in FindClose(hFind). In other words, as I understand this, you should copy (or duplicate) fdata.cFileName into files[i] on each iteration.

提交回复
热议问题