Reading from a CSV/text file with quotes in C++ -


i have working function reads lines text file (csv), need modify able read double quotes (i need have these double quotes because of string values contain commas, using double-quotes denote fact read function should ignore commas between double-quotes). there relatively simple way modify function below accommodate fact of fields enclosed in double quotes?

a few other notes:

  1. i have of fields enclosed in double-quotes if helps (rather ones strings, case)

  2. i change delimiter comma other character (like pipe), hoping stick csv if easy so

here current function:

void readloandata(vector<modelloandata>& mloan, int dealnum) {  // variable declarations fstream inputfile; string curfilename; ostringstream s1; string curlinecontents; int linecounter; char * cstr; vector<string> currow; const char * delim = ",";  s1 << "modelloandata" << dealnum << ".csv"; curfilename = s1.str(); inputfile.open(curfilename, ios::in);  if (inputfile.is_open()) {      linecounter = 1;     while (inputfile.good()) {         // grab line         while (getline (inputfile, curlinecontents)) {              // create c-style string can tokenize             cstr = new char [curlinecontents.length()+1];             strcpy (cstr, curlinecontents.c_str());              // need resolve "blank" token issue (strtok vs. strsep)             currow = split(cstr,delim);              // assign values our model loan data object             mloan[linecounter] = assignloandata(currow);              delete[] cstr;             ++linecounter;         }        }     // close input file     inputfile.close(); } else     cout << "error: file did not open" << endl; 

}

the following works given input: a,b,c,"a,b,c","a,b",d,e,f

#include <iostream> #include <sstream> #include <string> using namespace std;  int main() {     std::string line;     while(std::getline(cin, line, '"')) {         std::stringstream ss(line);         while(std::getline(ss, line, ',')) {             cout << line << endl;         }         if(std::getline(cin, line, '"')) {             cout << line;         }     } } 

Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -