How do I get a list of files in a directory in C++?

后端 未结 13 1440
南旧
南旧 2020-11-28 23:36

How do you get a list of files within a directory so each can be processed?

13条回答
  •  [愿得一人]
    2020-11-28 23:56

    You can use the following code for getting all files in a directory.A simple modification in the Andreas Bonini answer to remove the occurance of "." and ".."

    CString dirpath="d:\\mydir"
    DWORD errVal = ERROR_SUCCESS;
    HANDLE dir;
    WIN32_FIND_DATA file_data;
    CString  file_name,full_file_name;
    if ((dir = FindFirstFile((dirname+ "/*"), &file_data)) == INVALID_HANDLE_VALUE)
    {
        errVal=ERROR_INVALID_ACCEL_HANDLE;
        return errVal;
    }
    
    while (FindNextFile(dir, &file_data)) {
        file_name = file_data.cFileName;
        full_file_name = dirname+ file_name;
        if (strcmp(file_data.cFileName, ".") != 0 && strcmp(file_data.cFileName, "..") != 0)
        {
            m_List.AddTail(full_file_name);
        }
    }
    

提交回复
热议问题