c++ - while(getline(myReadFile, temp, ':')) executing one iteration too many causing out of bounds on vector -


i have question on std::readline(istream, string, delimiter). trying read in file , store data in struct , add map. have gotten work. while loop iterates 1 loop many causing vector have no data stored in causes assertion failure out of bounds.

i have read every stack overflow question on readline , none seem give me idea why behavior occurs. perhaps here can enlighten me.

if (myreadfile.is_open()) {     while(getline(myreadfile, temp, ':')){//loops through , splits line 1 delimiter @ time         stringvector.push_back(temp);//add vector         iteminfo tempitem = iteminfo(stringvector[1], stod(stringvector[2]), stod(stringvector[3]));//create struct         catalog.insert(pair<string,iteminfo>(stringvector[0], tempitem));//add map         stringvector.clear();     }  }      myreadfile.close(); 

thanks help

the present code should break @ first iteration.

assuming strart empty vection, read first line of file in tmp, fine. push_back tmp vector, still fine. vector contains 1 element of index 0.

but on next line try use elements of index 1, 2 , 3 vector contains 1 element => index out of bound exception guaranteed.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -