-5

I'm trying to read in a .csv file filled with strings for a program that I have. I wrote the code below to try to read each individual string.

string read_from_f(istream &in) {

string a;
char fill = '0';

while (fill != ',') {
    in.get(fill);

    if (fill != ',') {
        a += fill;
    }
}   

return a; 
}

This ends up not working because some of my strings have comma's in them and are structured like this. "........", "Donald, Trump", "....."

When I look at my locals when I debug, in.get() does not read in the double quotes from each string. Is there a way that I can read from quotes to quotes so I don't get any unexpected errors?

ChaseRun
  • 21
  • 1
  • 7
  • 1
    If you know for sure that every data value is a string, why not read until you use the double quotes as a delimiter instead of the comma? Then you'll just have to disregard every other result (which will just be a comma) and you'll have everything – scohe001 Aug 11 '17 at 22:38
  • 1
    You need to write, or at least use, a proper CSV parser - I have one here at https://bitbucket.org/neilb/csvparse/src - you can't do this simply using C++ Standard Library facilities. –  Aug 11 '17 at 22:38
  • 1
    Make an [appointment with your rubber duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging) to explain your proposed algorithm for parsing a CSV file. Do not attempt to write another line of code until you get your rubber duck's approval. – Sam Varshavchik Aug 11 '17 at 22:46
  • 1
    Please search before posting. There are already a plethora of similar posts on StackOverflow. Try using keywords "stackoverflow c++ read file csv" – Thomas Matthews Aug 11 '17 at 23:07
  • See `std::getline`, as in `std::getline(in, a, ',');` – Thomas Matthews Aug 11 '17 at 23:08
  • @Thomas "because some of my strings have comma's in them" –  Aug 11 '17 at 23:16
  • 1
    Possible duplicate of [How can I read and parse CSV files in C++?](https://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c) – Barmar Aug 11 '17 at 23:49
  • @Barmar all the top answers there assume "you don't care about escaping comma," which our OP clearly does. – scohe001 Aug 12 '17 at 00:15

2 Answers2

0

As suggested by scohe001, if your csv consists solely of strings, each enclosed in double quotes, a simple solution would be to use the double quote as delimiter and consider each second value provided. Reading in until a delimiter can be achieved easily with getline:

#include <iostream>

int main()
{
    ifstream f(DATAFILE);
    if (!f.is_open()) {
        cout << "error opening file." << endl;
        return 1;
    }
    string content;
    int count = 0;
    while(std::getline(f, content, '"')) {
        if (count%2 != 0) { // every second entry is actual content
            cout << content << endl;
        }
        count++;
    }
    return 0;
}
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
0

So I came up with a simple solution. I changed my .csv to a .txt file and instead of checking for commas, I checked for quotes. My in.get() was able to read in the quotes when the file was a .txt file.

ChaseRun
  • 21
  • 1
  • 7