1

I have a program that can successfully read a CSV file. The said CSV file is a list separated by 3 columns. And each column has a comma in between each line. For instance

line 1 --- artist, genre, song line 2 --- Michael jackson, Pop, thriller

and so on. What I want my program to do is read the first line and to take artist, genre, and song, print those out to the user as options. For instance

"Choose an Option" 1. Artist 2. Genre 3. Song

and then when they select an option it then shows them either all the artists, songs, or genres in the CSV file.

So far I have my program reading the CSV and putting each line in a vector. Here is my code so far ...

#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;
}

I'm just having trouble figuring out how to have it read the first line, separate each string in the first line that is already separated by a comma and have that then outputted to the user as an option. So in essence I can change artist, genre, song to something like appetizers, dish, drinks in the CSV file.

1

2 Answers 2

1

First you have to read the csv file and store the lines in a vector, You can use this function to do that.

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;
}

Then split the each line entry to 2D vector. for that you can use this function

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;
}

I haven't try to compile any of these function, because I do not have environment to do so here. But I think this will help to to think about the direction.

now your data in a 2D vector like excell sheet. So you can access by the index of the inner vector.

0

You could create a std::map<string,vector<string>> so that you could then store each column of the CSV file from line 2 onwards in the map as a vector of strings, keyed by the text that appears in the column on line 1 of the CSV file.

Then you could present the names of the fields by iterating through the keys of the std::map, which are conveniently stored in alphabetical order.

The task of reading in the first line of the CSV file could be performed by the code you already have, or something more sophisticated that takes into account the quoted fields, etc. that are found in fully-featured CSV files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.