C ++写文件错误

时间:2018-05-10 02:24:08

标签: c++ writefile

我的文字档案:

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.\n\n"; 
    }
    else {
        cout << "Could not open file.\n\n";
    }
}

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 << "Order\n_______\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 << "\n\nContinue 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 << "\n\nContinue to add to order?(y)";
        cin >> cont;
    }while(cont == 'y');

此时的任何事都有助于我。提前谢谢。

2 个答案:

答案 0 :(得分:0)

您对文件的实际输出显然来自重载的&lt;&lt;运算符定义为:

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

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

我一点也不清楚这行代码是什么

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

确实给出了你的&lt;&lt;运算符仅将流和引用作为参数。我原以为你会遇到编译错误。相反,我猜测&lt;&lt;函数被多次调用。

我建议摆脱你的重载&lt;&lt;和&gt;&gt;并且只需使用系统标准的读入和写出。

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

答案 1 :(得分:0)

没有大括号的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";
    }
}

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