title: Reading from a CSV/text file with quotes in C++
body: <p>I have a working function that reads lines from a text file (CSV), but I need to modify it to be able to read double quotes (I need to have these double quotes because some of my string values contain commas, so I am using double-quotes to denote the fact that the read function should ignore commas between the double-quotes).  Is there a relatively simple way to modify the function below to accommodate the fact that some of the fields will be enclosed in double quotes?</p>
  
  <p>A few other notes:</p>
  
  <ol>
  <li><p>I could have all of the fields enclosed in double-quotes fairly easily if that helps (rather than just the ones that are strings, as is currently the case)</p></li>
  <li><p>I could also change the delimiter fairly easily from a comma to some other character (like a pipe), but was hoping to stick with CSV if its easy to do so</p></li>
  </ol>
  
  <p>Here is my current function:</p>
  
  <pre><code>void ReadLoanData(vector&lt;ModelLoanData&gt;&amp; mLoan, int dealnum) {
  
  // Variable declarations
  fstream InputFile;
  string CurFileName;
  ostringstream s1;
  string CurLineContents;
  int LineCounter;
  char * cstr;
  vector&lt;string&gt; currow;
  const char * delim = ",";
  
  s1 &lt;&lt; "ModelLoanData" &lt;&lt; dealnum &lt;&lt; ".csv";
  CurFileName = s1.str();
  InputFile.open(CurFileName, ios::in);
  
  if (InputFile.is_open()) {
  
      LineCounter = 1;
      while (InputFile.good()) {
          // Grab the line
          while (getline (InputFile, CurLineContents)) {
  
              // Create a c-style string so we can tokenize
              cstr = new char [CurLineContents.length()+1];
              strcpy (cstr, CurLineContents.c_str());
  
              // Need to resolve the "blank" token issue (strtok vs. strsep)
              currow = split(cstr,delim);
  
              // Assign the values to our model loan data object
              mLoan[LineCounter] = AssignLoanData(currow);
  
              delete[] cstr;
              ++LineCounter;
          }   
      }
      // Close the input file
      InputFile.close();
  }
  else
      cout &lt;&lt; "Error: File Did Not Open" &lt;&lt; endl;
  </code></pre>
  
  <p>}</p>