Here is my function:
void loadfromfile(string fn, vector<string>& file){ int x = 0; ifstream text(fn.c_str()); while(text.good()){ getline(text, file.at(x)); x++; }
The fn value I pass in is just the name of the text file ("10a.txt") The value of the transferred file is declared as follows:
vector<string> file1;
The reason I didn’t determine the size is because I didn’t think I should have with vectors, they are dynamic ... right?
This function should read the given text file and store the full contents of each line in one vector cell.
Ex. Store the contents of the first line in file.at (0) Save the contents of the second line in file.at (1) And so on, until there are no more lines in the text file.
Mistake:
ending a call after calling an instance of 'std :: out_of_range' what (): vector :: _ M_range_check
I thought checking in a while loop should prevent this error!
Thanks in advance for your help.
c ++ vector outofrangeexception
Mark S.
source share