C++写入文件错误
C++ write file error
我的文本文件:
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";
}
}
这将包括条件中的所有代码。
相关文章:
- 用MacOS Mojave编译C++:致命错误:mpi.h:没有这样的文件或目录
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 如何在文件和行号中抛出错误
- 防止在C++中选择错误文件时提升 xml 解析器崩溃
- AMQP-CPP >处理程序中的错误文件描述符
- Xcode OSX上的C++构建失败,出现多个错误文件IO..不可用:在macOS 10.15中引入
- 为什么生成文件返回 g++: 错误: h 文件或目录 make: *** [生成文件:12: 测试] 错误 1?
- 警告:打开错误文件(/build/opencv/modules/videoio/src/cap_ffmpeg_impl.
- Ubuntu C++ 编译器错误: g++: 错误: 文件.cpp: 没有这样的文件或目录
- main.cc:5:30:致命错误:文件夹/file.h:没有这样的文件或目录
- C++ LNK2005"已定义"错误 - 文件引用自身
- C++:模拟餐饮公司计费程序-无法将错误数据输出到错误文件
- 博兰德C++链接器错误c文件包括
- Visual Studio 2015 代码覆盖率错误文件
- 使用system()与Gnuplot进行绘图会导致错误:文件不可读
- 是否存在fseek/ftell可能给出错误文件大小的情况
- g++编译错误.h文件
- c++编译错误:.o文件未找到
- 打开错误文件的错误消息
- Ndk-build输出添加符号错误.文件格式错误