Search code examples
c++csvatoi

Atoi function while reading a .csv file C++


The execution of my code crashes when it gets to the "atoi" function, but I cannot understand why. The code is supposed to read a matrix from a .csv file, considering: - the first row (so till the first '\n') and saving each element (separated by a ',') in a vector of ints; - the rest of the matrix, by looking at each element and creating a specific object if the number read is 1 or 2. I don't get any exception while debugging the program, it just crashes during the execution (and using the system ("PAUSE") I could figure out it was the atoi function which didn't work properly). Can you help me understand what is going wrong? Thank you very much. Ps: I also attached all the libraries I'm loading... maybe it can help :)

#include <fstream>
#include <stdio.h>
#include <sstream>
#define nullptr 0
#include <string>
#include "classi.h"
#include <cstdlib>
#include <stdlib.h>
#include <iostream>
#include <vector>
using namespace std;


int main(int argc, char *argv[]) {
    ifstream file("problem.csv");
    unsigned int N = 0;
    unsigned int M = 0;
    char c; //modificato char * c;
    unsigned int i=0,j=0, k=0, n_iter, j_temp =0;
    std::vector<car> v_row;
    std::vector<car> v_col_temp;
    std::vector<int> iter; // location where I want to save the ints corresponding to the elements of the first row of the .csv file //
    std::string iterazioni; //location where I want to save the first row as a string and then cut it into pieces (iteraz) and then convert (atoi --> iter)
    std::string iteraz;

    while(!file.eof()){
        std::getline(file,iterazioni,'\n');
        stringstream it(iterazioni);
        while (it.good()) {
            std::getline(it,iteraz, ',');
            iter[k] = atoi(iteraz.c_str());
            if(iter[k]<0){
                cout<<"Errore: negative #of iterations"<<endl;
                break;
            }
            iter.push_back(0);
            k++;
        }
        iter.pop_back();

        file.get(c);
        if (c=='1'){
            blue_car b(i,j);
            if (v_col_temp[i].get_next() != nullptr)
                v_col_temp[i].insert_tail(&b);
            else
                v_col_temp[i].insert_head(&b);
        }
        if (c=='2'){
            red_car r(i,j);
            if (v_row[i].get_next() != nullptr)
                v_row[i].insert_tail(&r);
            else
                v_row[i].insert_head(&r);
        }
        if (c==',') {
            j++;
            if (i == 0)
                j_temp++;
        }
        if (c=='\n'){
            car p;
            v_row.push_back(p);
            v_col_temp.push_back(p);
            i++;
            if (j != j_temp) {
                std ::cout<<"errore input non valido: numero righe/colonne non coerente"<<endl;
            }
            j=0;
        }

        else if ((c!='\n') && (c!=',') && (c!='0') && (c!='1') && (c!='2'))
            std ::cout<<"errore input non valido"<<endl;
    };
    n_iter = k-1;
    M=i;
    N=j+1;

...

Solution

  • Your program crashes because you failed to initialize the contents of the iter vector.

    std::vector<int> iter; // location where I want to save the ints corresponding to the elements of the first row of the .csv file //
    

    You declare and construct this vector. The vector is empty at this point, and it has no elements.

    At some point later:

    iter[k] = atoi(iteraz.c_str());
    

    The initial value of k is 0, so this attempts to assign the return value from atoi() to iter[0].

    The problem is, of course, there is no iter[0]. The iter vector is still empty, at this point.

    Additional comments, which is sadly true for at least 50% of these kinds of questions on stackoverflow.com:

    1) "using namespace std"; is a bad practice, that should be avoided

    2) Do not use system("pause") as well, as you referenced in your question.