I'm attempting to read an inventory in from 3 files in one function and then rewrite those 3 files in another. When I try each of these functions individually, they both seem to work. But when I use both of them in the same run, the text files either are empty or having missing info. Is there any way that code should be changed when reading and writing to the same file?
void Inventory::fromFile(std::string filename){
std::ifstream infile(filename);
if (infile){
if(filename == "game.txt"){
while (infile){
if(infile){
std::string strInput;
std::getline(infile, strInput);
if(strInput.length()>0){
std::stringstream splitter(strInput);
std::string title,price,copies,condition,genre,rating,maker;
getline(splitter,title,',');
getline(splitter,price,',');
getline(splitter,copies,',');
getline(splitter,condition,',');
getline(splitter,genre,',');
getline(splitter,rating,',');
getline(splitter,maker,',');
Game* g = new Game(stoi(copies), stof(price), title, genre,rating, to_bool(condition),maker);
gameStock.add(g);
std::cout << "\nRead:\t" << g->toString() << "\n";
}
}
}
}
if(filename == "console.txt"){
while (infile){
if(infile){
std::string strInput;
std::getline(infile, strInput);
if(strInput.length()>0){
std::stringstream splitter(strInput);
std::string title,price,copies,condition,edition,maker,warranty;
getline(splitter,title,',');
getline(splitter,price,',');
getline(splitter,copies,',');
getline(splitter,condition,',');
getline(splitter,edition,',');
getline(splitter,maker,',');
getline(splitter,warranty,',');
Console* c = new Console(stoi(copies), stof(price), title, edition,maker, stoi(warranty), to_bool(condition));
gameStock.add(c);
std::cout << "\nRead:\t" << c->toString() << "\n";
}
}
}
}
if(filename == "accessory.txt"){
while (infile){
if(infile){
std::string strInput;
std::getline(infile, strInput);
if(strInput.length()>0){
std::stringstream splitter(strInput);
std::string title,price,copies,condition,consoleTo,warranty;
getline(splitter,title,',');
getline(splitter,price,',');
getline(splitter,copies,',');
getline(splitter,condition,',');
getline(splitter,consoleTo,',');
getline(splitter,warranty,',');
Accessory* a = new Accessory(stoi(copies), stof(price), title, consoleTo, to_bool(condition), stoi(warranty));
gameStock.add(a);
std::cout << "\nRead:\t" << a->toString() << "\n";
}
}
}
}
}
else {
std::cout << "Can't read from file. Inventory not loaded.\n";
}
}
void Inventory::toFile(std::string filename){
std::ofstream outf(filename);
if (outf){
if(filename == "game.txt"){
for(int i = 0; i < numGames; i++){
ItemADT* curr = gameStock.get(i);
if(curr != nullptr){
outf << curr->fileFormat() + ",\n";
}
}
}
if(filename == "console.txt"){
for(int i = 0; i < numConsoles; i++){
ItemADT* curr = consoleStock.get(i);
if(curr != nullptr){
outf << curr->fileFormat() + ",\n";
}
}
}
if(filename == "accessory.txt"){
for(int i = 0; i < numAccessories; i++){
ItemADT* curr = acessStock.get(i);
if(curr != nullptr){
outf << curr->fileFormat() + ",\n";
}
}
}
outf.close();
}
else { // Print an error and exit
std::cout << "Unable to write to file.\n";
}
}