C++ reading from file and putting into vector

Discussion in 'General Computer Chat' started by Bananapie, Nov 2, 2011.

  1. Bananapie

    Bananapie New Member

    Messages:
    184
    I am having trouble with a C++ program which requires that you read in 3 different files (first contains integer, second contains float, and third contains string) into a vector, that doesn't specify the number of int's(ect) in the file. Then from there we make a heap, sort it and whatnot.

    I am just having trouble reading the integers from the file and putting them into separate elements.

    Code:
    template <class T> void get_list (vector <T>& v, const char* path)
    {//begin get_list
    
    ifstream infile(path);
    string i;
    int number;
    int j = 0;
    
    while (infile >> i)
    {//begin while
            stringstream myStream(i);
            myStream >> number;    //convert to int
            v[j] = number; //put int into vector[element j]
            j++; //update j to next element
    }//end while
    
    for (unsigned int k = 0; k < v.size(); k++)
            cout << v[k] << endl;
    
    }//end get_list
    

    That is what I have. I googled the code that I had to try and get it to work for integer, than adjust it to hopefully work for them all. I am getting a segmentation fault, and unsure why.


    This is the text file:

    Code:
      28    -647    -382      69     895    -655     404    -546
      -9    -749    -831    -220    -444    -263     966      71
     531     293     534     560     646    -695     251    -369
    -305     834      40    -197     213     571     863     739
     733     349     517     164    -220    -288    -598     654
    -167     -72     958    -746    -573     916     475    -181
     560     516     913    -942    -361     514    -513     179
    -912     912    -361    -880    -115     830     144    -761
     139    -495      -7    -525     -45    -187     746    -145
    -282    -235    -912    -677      45     393    -804    -197
     547    -509    -313    -539    -403    -390     774    -925
     302    -202     352     465     875    -532     677     934
     557    -136     348     618
    
    
     
  2. TrainTrackHack

    TrainTrackHack VIP Member

    Messages:
    3,672
    What's the exact error you're getting?

    Though this might not solve your problem, when sequentially inserting multiple values in a vector (or any STL array/list structure), it's generally preferable to use v.push_back(number) rather than v[j] = number and then incrementing j (push_back simply appends a value at the end of the vector). It's cleaner, easier to read, and automatically resizes the vector if it isn't big enough to hold the new item.
     
  3. Bananapie

    Bananapie New Member

    Messages:
    184
    You know what? That actually fixed my problem. I didn't even think about it resizing the vector. I just used v[j] = number because I was used to using arrays more, so it was just a comfort thing. I don't mind using push_back at all though.

    I was getting a segmentation fault with the code that I had, but switched it out with push_back and it worked just fine. Thanks a lot. Easy fix, I just didn't think about it :good:
     

Share This Page