windows C: 如何获取Windows目录下的文件列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6295156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:02:18  来源:igfitidea点击:

C: How to obtain a list of files in Windows directory?

cwindowswinapifileftp

提问by floader

I am trying to implement an FTP Server in C (school assignment), according to the RFC959 standard.

我正在尝试根据 RFC959 标准在 C(学校作业)中实现一个 FTP 服务器。

I am having trouble with the LIST command. The RFC reads: "This command causes a list to be sent from the server to the passive DTP. If the pathname specifies a directory or other group of files, the server should transfer a list of files in the specified directory. If the pathname specifies a file then the server should send current information on the file. A null argument implies the user's current working or default directory."

我在使用 LIST 命令时遇到问题。RFC 写道:“此命令会导致从服务器向被动 DTP 发送一个列表。如果路径名指定了一个目录或其他文件组,则服务器应传输指定目录中的文件列表。如果路径名指定一个文件,然后服务器应该发送有关该文件的当前信息。空参数表示用户当前的工作目录或默认目录。”

I know that there are functions like GetCurrentDirectory, etc. Is there a function to obtain an ouput such as that of 'dir' in MS-DOS command prompt? Anything just similiar would be helpful.

我知道有 GetCurrentDirectory 等函数。是否有函数可以在 MS-DOS 命令提示符下获取诸如“dir”之类的输出?任何类似的东西都会有帮助。

Thanks in advance!

提前致谢!

回答by Alex K.

FindFirstFile& FindNextFileare the APIs to enumerate a path.

FindFirstFile&FindNextFile是用于枚举路径的 API。

回答by netboffin

Adrian Worley wrote a tutorial explaining how to get a list of files in a directory using FindFirstFile and FindNextFile http://www.adrianxw.dk/SoftwareSite/FindFirstFile/FindFirstFile1.html

Adrian Worley 写了一篇教程,解释了如何使用 FindFirstFile 和 FindNextFile http://www.adrianxw.dk/SoftwareSite/FindFirstFile/FindFirstFile1.html获取目录中的文件列表

Here's a small example.

这是一个小例子。

#include <windows.h> 
#include <iostream> 
using namespace std;

int main()
{
    HANDLE hFind;
    WIN32_FIND_DATA FindData;

    cout << "FindFirstFile/FindNextFile demo.\n" << endl;

    // Find the first file

    hFind = FindFirstFile("C:\Windows\*.exe", &FindData);
    cout << FindData.cFileName << endl;

    // Look for more

    while (FindNextFile(hFind, &FindData))
    {
        cout << FindData.cFileName << endl;
    }

    // Close the file handle

    FindClose(hFind);

    return 0;
}