C++写入文件错误

C++ write file error

本文关键字:错误 文件 C++      更新时间:2023-10-16

我的文本文件:

1:Meat Dish:Steak:11.5  
2:Fish Dish:Fish and chips:12

所以基本上我正在尝试读取一个文件,通过选择 1 或 2 来选择一道菜,然后将名称和价格写入文件。

这是我的代码:

#include <iostream>
#include <fstream>
#include <string>
#include <vector> // We will use this to store Players
using std::string;
using std::ofstream; 
using std::ifstream; 
using std::cout;
using std::cin;
using std::vector;
struct MenuList { // Define a "Menuu" data structure
string itemNo;
string category;
string descript;
double price;
};
std::istream& operator>>(std::istream& infile, MenuList& menu) {

getline(infile, menu.itemNo, ':'); 
getline(infile, menu.category, ':');
getline(infile, menu.descript, ':');
infile >> menu.price;
// When we have extracted all of our information, return the stream
return infile;
}
std::ostream& operator<<(std::ostream& os, MenuList& menu) {
os << "" << menu.itemNo << " " << menu.category << " - " <<
menu.descript;
// When we have extracted all of our information, return the stream
return os;
}
void Load(vector<MenuList>& r, string filename) {
std::ifstream ifs(filename.c_str()); // Open the file name
if(ifs) {
while(ifs.good()) { // While contents are left to be extracted
MenuList temp;
ifs >> temp;        // Extract record into a temp object
r.push_back(temp);  // Copy it to the record database
}
cout << "Read " << r.size() << " records.nn"; 
}
else {
cout << "Could not open file.nn";
}
}
void Read(vector<MenuList>& r) {// Read record contents
for(unsigned int i = 0; i < r.size(); i++)
cout << r[i] << "n";
}
void Search(vector<MenuList>& r) {// Search records for itemNo
string n;
int a;
char cont;
cout << "Ordern_______n";
do {
cout << "Enter quantity: ";
cin >> a;
cout << "Enter dish: ";
cin >> n;
for(int i = 0; i < r.size(); i++) {
if(r[i].itemNo.find(n) != string::npos) 
cout << r[i].category << " - " <<r[i].descript << ' ' <<
a*r[i].price;
std::ofstream ofs;
ofs.open ("transactions.txt", std::ofstream::out | std::ofstream::app);
ofs << r[i].category << " - " <<r[i].descript << ' ' << a*r[i].price << "n";
}
cout << "nnContinue to add to order?(y)";
cin >> cont;
}while(cont == 'y');
}
int main() {
vector<MenuList> records;
Load(records, "delete.txt");
Read(records);
Search(records);
return 0;
}

当我输入要在屏幕上显示的菜肴并写入文件时,在屏幕上显示菜肴效果很好,但是当它将菜肴写入文件时,即使我只选择了 1,它也会同时写入它们。

如果我选择第一个菜,预期的输出到文件中:Meat Dish - Steak 11.5
但我得到的是:

1:Meat Dish:Steak:11.5
2:Fish Dish:Fish and chips:12

问题出在这里的某个地方:

do {
cout << "Enter quantity: ";
cin >> a;
cout << "Enter dish: ";
cin >> n;
for(int i = 0; i < r.size(); i++) {
if(r[i].itemNo.find(n) != string::npos) 
cout << r[i].category << " - " <<r[i].descript << ' ' <<
a*r[i].price;
std::ofstream ofs;
ofs.open ("transactions.txt", std::ofstream::out | std::ofstream::app);
ofs << r[i].category << " - " <<r[i].descript << ' ' << a*r[i].price << "n";
}
cout << "nnContinue to add to order?(y)";
cin >> cont;
}while(cont == 'y');

在这一点上,任何事情都对我有帮助。提前谢谢你。

文件的实际输出显然来自您定义的重载<<运算符:

std::ostream& operator<<(std::ostream& os, MenuList& menu) {

在您的第一个代码片段中,因为它以数字开头。

我完全不清楚这行代码是什么

ofs << r[i].category << " - " <<r[i].descript << ' ' << a*r[i].price << "n";

考虑到您的<<运算符仅将流和引用作为参数的事实。我本来以为你会得到一个编译错误。相反,我猜 <<函数被多次调用。

我建议摆脱超载的<<和>>,只需使用系统标准来读取和写出。

或者,也许,使用重载运算符,但将它期望的对象传递给它。

不带大括号的 if 语句只会影响以下代码行。因此,只有以下行受条件影响。

if(r[i].itemNo.find(n) != string::npos) 
cout << r[i].category << " - " <<r[i].descript << ' ' << a*r[i].price;

为了仅将满足条件的条目写入文件,您可以按如下方式添加大括号:

for(int i = 0; i < r.size(); i++) {
if(r[i].itemNo.find(n) != string::npos){
cout << r[i].category << " - " <<r[i].descript << ' ' << a*r[i].price;
std::ofstream ofs;
ofs.open ("transactions.txt", std::ofstream::out | std::ofstream::app);
ofs << r[i].category << " - " <<r[i].descript << ' ' << a*r[i].price << "n";
}
}

这将包括条件中的所有代码。