struct Stat{ unsigned short ID; string Code; string Name; string Description;}
How would I read values in from a text file and populate a vector of type Stat? I've tried variations of the following with no luck:
ifstream file; string line; size_t found; int startPos = 1; vector<Stat>::iterator i; file.open(FILE_STATS); // defined constant for file name if (file.is_open()) { while (file >> line) { _stats.push_back(Stat()); i = _stats[_stats.size()]; found = line.find("|"); if (found != string::npos) { i.ID = (short int)line.substr(startPos, found - 1); startPos = found + 1; } found = line.find("|", found); if (found != string::npos) { i.Name = line.substr(startPos, found - startPos - 1); startPos = found + 1; } i.Description = line.substr(startPos, line.length()); }
I'm sure part of the problem is my lack of knowledge in the use of vectors, so could someone help me out with their correct usage?