mardi 10 septembre 2019

getline() skips first line and add a bunch of random numbers at the end when reading from a file [duplicate]

This question already has an answer here:

I have a text file which has a number on each line. I'm trying to read the file line by line and store it in an array, but getline() skips the first number and also adds some random numbers at the end, any suggestions how to fix this ?

int* data = new int[numElements];
    int i = 0;
    ifstream in2(argv[1]);
    while(getline(in2, line))
    {
        in2 >> data[i];
        ++i;
    }
    in2.close();

Aucun commentaire:

Enregistrer un commentaire