int main(){
vector<string> strs;
FILE* f = _popen("tasklist", "r");
char str[90];
while (fgets(str, 90, f)) {
strs.push_back(str);
}
for (string& x : strs) {
cout << x;
x.pop_back();
cout << x<<endl;
x.pop_back(); //error
cout << x<<endl<<endl;
}
_pclose(f);
cin.sync();
cin.ignore();
return 0;
}
The pop_back() call from line 21 causes unhandled exception std::out_of_range. Doing x.erase(x.end() - 2, x.end()); instead of the two pop_backs causes the same thing. This means there is a problem with the forelast character in x. What is wrong ?
What a shame, that was the problem indeed. It seems the first string returned from "tasklist" command is only containing a newline character. I had no idea.