I want my program to be able to remember where it left off in a .txt
file in order to proceed to the next input upon reiteration through a loop.
For instance, a text file containing:
Apples
Bananas
Oranges
would be accessed through a function GetItem()
that appends the next file input into a vector of items. How do I make the function add Apples
the first time, Bananas
the second time, and Oranges
the third iteration? As of now, each call to GetItem()
keeps adding the first element to the vector, giving a vector containing:
Apples
Apples
Apples
Because the file keeps opening from the beginning. Any help would be appreciated.
This is a simplified version of lengthy amounts of code that I could include, but would distract from the main purpose of the question. If the code is needed, I would be happy to include it.
vector<Item*> AddItemToInventory(vector<Item*> inventory) {
if (inptLctn == 'f') {
inptFile.open("TestFood.txt");
if (!inptFile.is_open()) {
cout << "Could not open file." << endl;
exit(1);
}
inptFile >> usrInptName;
inptFile >> usrInptQnty;
inptFile >> usrInptExpr;
inptFile >> usrInptPrice;
}
prdc = new Produce;
prdc->SetName(usrInptName);
prdc->SetQuantity(usrInptQnty);
prdc->SetExpiration(usrInptExpr);
prdc->SetPrice(usrInptPrice);
inventory.push_back(prdc);
return inventory;
}