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.
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:
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