
//
// G. Hagopian
// Merging two CSV files
//

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>

//------------------------------------------------------------------------------

using namespace std;

bool isComma(char c) {
    if(c==',') return true;
    return false;
}

vector<string> getCSVstrings(string inStr) {
    char ch;
    string s;
    vector<string> vs;
    for(int i = 0; i < inStr.length()+1; ++i) {
        ///keep getting characters in s until ch==','
        if(inStr[i]==',' || inStr[i] == NULL) {
            vs.push_back(s);
            s.erase();
        }
        else {
            s += inStr[i];
        }
    }
    return vs;
}

int main() {
    string fname, str;
    cout << "\nEnter a file name: ";
    cin >> fname;            /// dog00.csv, for instance
    fstream ifs(fname.c_str());
    if(!ifs) cout << "\nYou botched that, buddy!";
    while(!ifs.eof()) {
        getline(ifs,str);      // header of csv file
        vector<string> csvRow;
        csvRow = getCSVstrings(str);
        //for(string s: csvRow) cout << s << ",";
        for(vector<string>::iterator it = csvRow.begin(); it != csvRow.end()-1; ++it) {
            cout << *it << ',';
        }
        cout << csvRow[csvRow.size()-1]; /// *(csvRow.begin()) ???
        cout << endl;
    }

    /*stringstream ss(name);
    ss>>first_name;         // input Dennis
    ss>>second_name;        // input Ritchie

    {
        string command;
        getline(cin,command);   // read the line

        stringstream ss(command);
        vector<string> words;
        string s;
        while (ss>>s) words.push_back(s);    // extract the individual words
    }*/
}

//------------------------------------------------------------------------------


