0

I'm trying to read a CSV file that looks like this:screenshot of a CSV file

When i read a line that says "Cliente" I want to skip it and read the next line. When i read a line "Itinerarios" I also want to skip it and read all the lines untill the next "Cliente" line. Basically Cliente is a class and Itinerario is a different class that is associated with Cliente, that's why i'm trying to read it that way.

I tried using ss.ignore() in this context but it seems to do nothing: My line is still "Cliente" and the code cant extract the values.

    if (fe.good()) {
    while (!fe.eof()) {
        getline(fe, linea);
        stringstream ss;

        if (linea != "") {
            ++total;

            if (total > 2) {
                if (linea == "Cliente") {


                    ss << linea;
                    ss.ignore(20,'\n');

                    // Starts reading Cliente stuff
                    getline(ss, dni, ';');


                    getline(ss, pass, ';');

EDIT: Here's a reduced example:

int main(int argc, char** argv) {

ifstream fe;
string linea;
int total = 0; // Counts lines

string dni;

//Reads the file
fe.open("ClientesItinerarios.csv");

if (fe.good()) {
    while (!fe.eof()) {
        getline(fe, linea); //Toma una l�nea del fichero
        stringstream ss;

        if (linea != "") {
            ++total;

            if (total > 2) {
                if (linea == "Cliente") {


                    ss << linea;
                    ss.ignore(20,'\n');

                    // Read only the first value to make the example shorter
                    getline(ss, dni, ';'); //Stores the first part into dni

                }

            }

        }

    }
    fe.close();
} else {
    cerr << "Cannot read file" << endl;
}

return 0;

}

and if it helps heres a screenshot of my debugger when its over getline(ss, dni, ';');

KnightCifer
  • 21
  • 1
  • 5
  • Please show a [mre], it doesn't appear that `ss` contains any newlines? – Alan Birtles Nov 13 '19 at 18:49
  • dealing with all the edge cases in CSV parsing isn't easy, I'd recommend using a library instead, e.g https://github.com/ben-strasser/fast-cpp-csv-parser – Alan Birtles Nov 13 '19 at 19:01
  • Thanks, i just added a minimal reproducible example and a debugger screenshot just in case. Since this is university homework i cant use a external library – KnightCifer Nov 13 '19 at 19:06
  • `ss` only contains `"Cliente"`, you need to read the next line from the file then call `ss.str(line)`, I don't think you need `ignore` at all – Alan Birtles Nov 13 '19 at 19:13
  • Yeah but how do i skip the next line? – KnightCifer Nov 13 '19 at 19:14
  • you've already read the `"Cliente"` line, you don't need to skip it – Alan Birtles Nov 13 '19 at 19:16
  • Unrelated but possible source of errors: `while (!fe.eof()) { ... }` should be `while(getline(fe, linea)) { ... } ` See also [this question](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – churill Nov 13 '19 at 19:49

0 Answers0