簡體   English   中英

使用getLine在C ++中讀取CSV文件中的單獨列

[英]Reading separated columns in a CSV file in C++ using getLine

我有一個可以成功讀取CSV文件的程序。 所述CSV文件是由三列分開的列表。 每列之間的每行之間都有一個逗號。 例如

第1行---藝術家,流派,歌曲第2行---邁克爾·傑克遜,流行音樂,驚悚片

等等。 我要我的程序做的是閱讀第一行,並選擇藝術家,流派和歌曲,將其打印出來作為選項。 例如

“選擇一個選項” 1.藝術家2.類型3.歌曲

然后,當他們選擇一個選項時,它將向他們顯示CSV文件中的所有歌手,歌曲或流派。

到目前為止,我的程序已讀取CSV並將每一行放入向量中。 到目前為止,這是我的代碼...

#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
    ifstream infile("music_list.csv");
    string line = "";
    vector<string> all_words;
    cout << "Hello";
    while (getline(infile, line))
    {
        stringstream strstr(line);
        string word = "";
        while (getline(strstr,word, ','))
        {
            all_words.push_back(word);
        }

        for (unsigned i = 0; i < all_words.size(); i++)
        {
            cout << all_words.at(i)<< "\n";
        }

    }
    system("pause");
    return 0;
}

我只是很難弄清楚如何讀取第一行,將第一行中的每個字符串都已經用逗號分隔,然后將其作為選項輸出給用戶。 因此,從本質上講,我可以在CSV文件中將藝術家,流派,歌曲更改為開胃菜,菜餚和飲料。

首先,您必須閱讀csv文件並將這些行存儲在矢量中,您可以使用此功能來做到這一點。

vector<string> readCsvFileContent(const string file)
{
        vector<string> buffer;
        ifstream configFile;
        configFile.exceptions(ifstream::badbit);
        try
        {
            configFile.open(file.c_str(),ifstream::in);
            if(configFile.is_open())
            {
                string line;                
                while (getline(configFile,line))
                {
                    buffer.push_back(line);
                }
                configFile.close();
            }           
        }
        catch (ifstream::failure e){            
            throw e;
        }
        return buffer;
}

然后將每個行條目拆分為2D向量。 為此,您可以使用此功能

vector<vector<string>> processCsvList(vector<string> csvList)
{
    #define SINGER_CONFIG_COUNT 3 //number of comma separated data suppose to be in one line.
    #define DELIMITED_CHAR ","
    #define EMPTY_STRING "";

    vector<vector<string>> tempList;
    string configCell ="";
    for(vector<string>::iterator it = csvList.begin(); it != csvList.end(); ++it)
    {
        if(*it == EMPTY_STRING)
        {
            continue;
        }
        stringstream  configLine(*it);
        vector<string> tempDevice;
        for(int i=0; i<SINGER_CONFIG_COUNT; i++)
        {
            if(getline(configLine,configCell,DELIMITED_CHAR))
            {               
                tempDevice.push_back(configCell);
            }else
            {
            tempDevice.push_back(EMPTY_STRING);
            }
        }
        tempList.push_back(tempDevice);
    }
    return tempList;
}

我沒有嘗試編譯任何這些函數 ,因為我在這里沒有環境可以這樣做。 但是我認為這將有助於思考方向。

現在,您的數據以2D向量(例如excell工作表)的形式出現。 因此,您可以通過內部向量的索引進行訪問。

您可以創建一個std::map<string,vector<string>>以便隨后將CSV文件的每一列從地圖的第2行開始存儲為字符串向量,由列中顯示的文本作為鍵在CSV文件的第1行上。

然后,您可以通過迭代std::map的鍵來顯示字段的名稱,這些鍵以字母順序方便地存儲。

讀取CSV文件第一行的任務可以由您已有的代碼執行,也可以通過考慮功能齊全的CSV文件中帶引號的字段等更為復雜的代碼來執行。

暫無
暫無

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

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