zhihuayiyuan 2023-07-05 14:06 采纳率: 20%
浏览 31
已结题

c++ 遍历文件时出现不读取文件夹的情况


#include <stdio.h>
#include <Windows.h>
using namespace std;
void FindFile(const char* filename)//函数语句:找出全盘文件
{   
    char tempPath[1000] = { 0 };
    sprintf(tempPath, "%s\\*.*", filename);

    WIN32_FIND_DATA finddata;//文件属性
    HANDLE hfile = FindFirstFile(tempPath, &finddata);
    while (1)
    {
        if (finddata.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)//文件夹
        {
            if (finddata.cFileName[0] != '.')
            {
                sprintf(tempPath, "%s\\%s", filename, finddata.cFileName);
                printf("%s——————————————————\n", tempPath);
                FindFile(tempPath);
            }
        }
        else//文件
        {
            sprintf(tempPath, "%s\\%s", filename, finddata.cFileName);
            printf("%s****************此处不读\n", tempPath);
        }
        if (FindNextFile(hfile, &finddata) == 0)
            break;
    }
}
int main()
{
    char path[5] = { 0 };
    unsigned alldisk;
    printf("主人:\n正在为您检测U盘的拔插喵喵喵喵喵喵。。。。。。\n");
    while(1)
    {
        alldisk = GetLogicalDrives();
        for (int i = 0; i < 10; i++)
        {
            if ((alldisk & 1) == 1)
            {
                sprintf(path, "%c:", 'A' + i);                
            }
            alldisk = alldisk >> 1;
            }
        if (GetDriveType(path) == DRIVE_REMOVABLE)
            break;
    }
    printf("主人:\nU盘:%s已经插入喵", path);
    FindFile("F:\Documents");


    return 0;
}


一:
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/971609635886131.png "#left")
文件夹Documents中存在文件,不读取其中的文件。
二:

![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/022599635886121.png "#left")






![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/19000073588618.png "#left")
读取study文件夹里面的文件,但是不读取videos里面的文件。
求求大佬解答
  • 写回答

2条回答 默认 最新

  • threenewbee 2023-07-05 14:09
    关注

    递归一下

    void FindFile(const char* filename)
    {
        char tempPath[1000] = { 0 };
        sprintf(tempPath, "%s\\*.*", filename);
    
        WIN32_FIND_DATA finddata;
        HANDLE hfile = FindFirstFile(tempPath, &finddata);
        if (hfile == INVALID_HANDLE_VALUE)
        {
            printf("无法打开目录: %s\n", filename);
            return;
        }
    
        do
        {
            if (finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                if (strcmp(finddata.cFileName, ".") != 0 && strcmp(finddata.cFileName, "..") != 0)
                {
                    sprintf(tempPath, "%s\\%s", filename, finddata.cFileName);
                    printf("%s——————————————————\n", tempPath);
                    FindFile(tempPath);
                }
            }
            else
            {
                sprintf(tempPath, "%s\\%s", filename, finddata.cFileName);
                printf("%s****************此处不读\n", tempPath);
            }
        } while (FindNextFile(hfile, &finddata) != 0);
    
        FindClose(hfile);
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月13日
  • 已采纳回答 7月5日
  • 修改了问题 7月5日
  • 修改了问题 7月5日
  • 展开全部