I have a project where I'm trying to save and load data from a file. I can save data to files just fine, however I have a couple questions about reading them. In my project I have a map of type map<string, vector<string>>. The key returns a vector of strings. In my project the key is a name of a holiday and the elements in the vector are different magicians available on that holiday. I'm saving my data to a file in this format:
The key is delimited from the vector using a tab. Then the elements of the vector are delimited from each other using pipes. The problem I'm running into is how do I know when I reach the last element of the vector? The number of elements in each vector varies and I won't know the size of it beforehand. I have the following code so far, but I'm not sure how to know when I've reached the end of the line.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void loadLookupByHolidayMap() {
ifstream lookupByHolidayFile;
lookupByHolidayFile.open("lookupByHoliday.txt");
while (!lookupByHolidayFile.eof()) {
string key, magician;
getline(lookupByHolidayFile, key, '\t'); // input data as key until a tab is found
//pseudocode...
while (not end of line) {
getline(lookupByHolidayFile, magician, '|');
myMap[key].push_back(magician); // add a magician to the vector
}//now move onto the next line (or map key), and repeat.
}
lookupByHolidayFile.close();
}
If someone could point me in the right direction that'd be great.
Ok thank you. What do you mean by my global variables? I see only one variable in my code above that wasn't declared in the scope of the function and it's a map, myMap. Is having containers as global throughout the program considered bad practice?