如何使用c++列出Windows目录下的所有CSV文件

How to list all CSV files in a Windows Directory using C++?

本文关键字:CSV 文件 c++ 何使用 列出 Windows      更新时间:2023-10-16

我对c++有点陌生,我必须列出Windows目录下的所有CSV文件,我在谷歌上搜索了一下,发现了很多列出目录中所有文件的方法我想出了以下解决方案:

int listFiles(string addDir, vector<string> &list) {
    DIR *dir = 0;
    struct dirent *entrada = 0;
    int isFile = 32768;
    dir = opendir(addDir.c_str());
    if (dir == 0) {
        cerr << "Could not open the directory." << endl;
        exit(1);
    }
    while (entrada = readdir(dir))
        if (entrada->d_type == isFile)
        {
        list.push_back(entrada->d_name);
        cout << entrada->d_name << endl;
        }
    closedir(dir);
    return 0;
}

它正在使用Windows的direction .h(我使用VS2013),但问题是:—设置isFile = 32768是否正确?它会一直在Windows上运行吗?—如何判断是否为CSV文件?

另一件事,我试图使用windows.h/FindNextFile,但它没有工作。使用FindNextFile或上述解决方案更好吗?

我猜FindNextFile只列出CSV文件会更容易,但我不知道怎么做。

我的exit应该是一个字符串,因为它是一个读取CSV文件的函数的输入。

谢谢。

int listFiles(const string& addDir, vector<string> &list, const std::string& _ext) {
    DIR *dir = 0;
    struct dirent *entrada = 0;
    int isFile = 32768;
    std::string ext("." + _ext);
    for (string::size_type i = 0; i < ext.length(); ++i)
        ext[i] = tolower(ext[i]);
    dir = opendir(addDir.c_str());
    if (dir == 0) {
        cerr << "Could not open the directory." << endl;
        exit(1);
    }
    while (entrada = readdir(dir))
    if (entrada->d_type == isFile)
    {
        const char *name = entrada->d_name;
        size_t len = strlen(entrada->d_name);
        if (len >= ext.length()) {
            std::string fext(name + len - ext.length());
            for (string::size_type i = 0; i < fext.length(); ++i)
                fext[i] = tolower(fext[i]);
            if (fext == ext) {
                list.push_back(entrada->d_name);
                cout << entrada->d_name << endl;
            }
        }
    }
    closedir(dir);
    return 0;
}
int main()
{
    vector<string> flist;
    listFiles("c:\", flist, "csv");
    system("PAUSE");
}

如果你想使用FindNextFile, msdn有一个例子来枚举目录中所有你可以适应的字段。


编辑:扩展windows API方法:

argv的类型为TCHAR*,根据#ifdef UNICODE的不同,可以是char*wchar_t*。它是所有Windows API调用使用的类型,它接受字符串参数。要创建TCHAR字面值,可以使用TEXT("text")。要创建wchar_t字面值,可以使用L"text"。如果不想使用TCHAR语义,可以将main重新定义为int main(int argc, char* argv)int wmain(int argc, wchar_t* arv)类型。在两种类型之间进行转换涉及到处理unicode和代码页,您可能应该使用第三方库。

将ASCII (std::stringchar*, char点在0-127之间)转换为unicode(std::wstringwchar_t*)只需创建一个std::wstring(std::string.cbegin(), std::string.cend())即可。

下面是一个代码示例,演示如何使用WinAPI函数列出目录中的文件:

#include <windows.h>
#incldue <string>
#include <iostream>
#ifdef UNICODE
typedef std::wstring tstring;
#else
typedef std::string tstring;
#endif
#ifdef UNICODE
std::wostream& tcout = std::wcout;
std::wostream& tcerr = std::wcerr;
#else 
std::ostream& tcout = std::cout;
std::ostream& tcerr = std::cerr;
#endif
int listFiles(const tstring& directory, std::vector<tstring> &list, const tstring& extension)
{
    using std::endl;
    WIN32_FIND_DATA file;
    HANDLE hListing;
    int error;
    tstring query;
    if (directory.length() > MAX_PATH - 2 - extension.length())
        tcerr << "directory name too long" << endl;
    query = directory + TEXT("*.") + extension;
    hListing = FindFirstFile(query.c_str(), &file);
    if (hListing == INVALID_HANDLE_VALUE) {
        error = GetLastError();
        if (error == ERROR_FILE_NOT_FOUND)
            tcout << "no ." << extension << " files found in directory " << directory << endl;
        return error;
    }
    do
    {
        if ((file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
        {
            tcout << file.cFileName << endl;
            list.push_back(file.cFileName);
        }
    } while (FindNextFile(hListing, &file) != 0);
    error = GetLastError();
    if (error == ERROR_NO_MORE_FILES)
        error = 0;
    FindClose(hListing);
    return error;
}
int _tmain(int argc, TCHAR* argv[])
{
    std::vector<tstring> files;
    listFiles(TEXT("C:\"), files, TEXT("sys"));
    if (argc > 1)
        listFiles(argv[1], files, TEXT("csv"));
}

如果你想简化它,你可以通过删除所有的T(TCHAR, TEXT(),新定义的tstring, tout, tcerr)变体并使用纯宽或非宽类型(如:char*,字符串,简单字面量,cout或wchar_t*, wstring, L"字面量,wcout)。如果你这样做,你需要使用WINAPI函数的特殊函数(即FindFirstFileA用于非宽和FindFirstFileW用于宽)