![](/img/trans.png)
[英]c++ File Handling error: Write and Read from same file and same program
[英]C++ Unable to read from file and write to file in the same run
我试图通过一个功能从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";
}
}
当我测试它时,如果我打开一个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();
当我完成阅读后打开ofstream时,它工作正常。 在读取之后以及在再次打开文件进行其他操作之前不关闭文件可能是一个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.