如何使用Windows API列出目录中的文件? [英] How to list files in a directory using the Windows API?

查看:237
本文介绍了如何使用Windows API列出目录中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码,它显示包含目录本身而不是目录内容的文件夹.我想显示其内容.我不想使用boost :: filesystem.

I have this code and it displays the folder with the directory itself and not its contents. I want to display its contents. I don't want to use boost::filesystem.

我该如何解决?

代码:

#include <windows.h>
#include <iostream>

int main()
{
    WIN32_FIND_DATA data;
    HANDLE hFind = FindFirstFile("C:\\semester2", &data);      // DIRECTORY

    if ( hFind != INVALID_HANDLE_VALUE ) {
        do {
            std::cout << data.cFileName << std::endl;
        } while (FindNextFile(hFind, &data));
        FindClose(hFind);
    }
}

输出:

semester2

推荐答案

HANDLE hFind = FindFirstFile("C:\\semester2", &data);       // DIRECTORY

您已找到目录,因为这就是您所要的.如果您需要这些文件,请要求它们:

You got the directory because that's what you asked for. If you want the files, ask for them:

HANDLE hFind = FindFirstFile("C:\\semester2\\*", &data);  // FILES

(如果愿意,可以改为使用*.*,但是显然这仅是因为向后兼容的破解,所以应该避免.请参见评论和RbMm的答案.)

(You can instead use *.* if you prefer, but apparently this only works because of a backwards compatibility hack so should probably be avoided. See comments and RbMm's answer.)

这篇关于如何使用Windows API列出目录中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆