C ++无法在同一次运行中从文件读取和写入文件

时间:2015-12-13 00:08:50

标签: c++ file-io readfile writefile

我试图从一个函数中的3个文件中读取清单,然后在另一个函数中重写这3个文件。当我单独尝试这些功能时,它们似乎都可以工作。但是当我在同一次运行中使用它们时,文本文件都是空的或缺少信息。在读取和写入同一文件时,是否有任何方法可以更改代码?

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";
}
}

1 个答案:

答案 0 :(得分:0)

当我测试它时,如果我打开ifstream和ofstream,然后使用它没有读取的ifstream读取,但它会写。

ifstream fin("test.txt");
ofstream fout("test.txt");
string str;

fin >> str;    
cout << str << endl;

str = "foo";
fout << str;

fin.close();
fout.close();
当我完成阅读时,当我打开游戏时,它工作正常。读取后不关闭文件,再次打开文件进行其他操作之前,可能会出现问题。