C: How to obtain a list of files in Windows direct

2019-08-15 18:58发布

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

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."

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.

Thanks in advance!

2条回答
Root(大扎)
2楼-- · 2019-08-15 19:31

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

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;
}
查看更多
我想做一个坏孩纸
3楼-- · 2019-08-15 19:47

FindFirstFile & FindNextFile are the APIs to enumerate a path.

查看更多
登录 后发表回答