簡體   English   中英

如何使用C ++列出Windows目錄中的所有CSV文件?

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

我對C ++有點陌生,我必須在Windows目錄中列出所有CSV文件,我已經用Google搜索過,發現了很多方法可以列出目錄中的所有文件,並且提出了以下解決方案:

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上使用dirent.h(我正在使用VS2013),但問題是:-設置isFile = 32768是否正確? 它將始終在Windows上運行嗎? -如何知道該文件是否為CSV文件?

另一件事,我嘗試使用windows.h / FindNextFile,但是沒有用。 使用FindNextFile還是上述解決方案更好?

我想FindNextFile僅列出CSV文件會更容易,但是我不知道該怎么做。

我的出口應該是一個字符串,因為它是讀取CSV文件的函數的輸入。

Tks的家伙。

PS:我不能使用升壓...

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* ,表示char*wchar_t*取決於#ifdef UNICODE 所有帶有字符串參數的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和代碼頁,您可能應該使用第3方庫。

從ASCII( std::stringchar* ,char點在0-127之間)轉換為unicode( std::wstringwchar_t*是創建std::wstring(std::string.cbegin(), std::string.cend())的簡單問題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,tcout,tcerr)變體並使用純寬或非寬幅格式,使應用程序鎖定在unicode中或完全不了解unicode。寬類型(即char *,字符串,簡單文字,cout或wchar_t *,wstring,L“”文字,wcout)。 如果執行此操作,則需要使用WINAPI函數的專用函數(即,對於非寬范圍使用FindFirstFileA,對於寬范圍使用FindFirstFileW)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM