mercredi 30 septembre 2020

How correctly read data from file and insert to linked list in c++? [duplicate]

I have a file with numbers in a file( one number per line), and I need to insert it to the linked list, but I think the error (libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: stoi: no conversion Abort trap: 6) is happening because of the conversion function

void MyLinkedList::addFront(const int& elem){
some code
}
void MyLinkedList::loadData(string path)
{
    
    ifstream infile;
    //string path;
    infile.open("file1.txt");
    Node *current = head;
    

    while(!infile.eof())
    {
    getline(infile, path);
    cout << path<<endl;
    int a=stoi(path);
    addFront(a);
    
    //current->elem = stoi(path);
    //current=current->next;
    }
    infile.close();
    
}

my addFront function works correctly, but i think it reads empty string and crushes. What should i do, Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire