【问题标题】:C++ write file errorC++写入文件错误
【发布时间】:2018-05-10 11:55:19
【问题描述】:

我的文本文件:

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');

此时的任何事情都对我有帮助。提前谢谢你。

【问题讨论】:

    标签: c++ writefile


    【解决方案1】:

    您对文件的实际输出显然来自您定义的重载

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

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

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

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

    确实考虑到您的

    我建议去掉你的重载的 > 并简单地使用系统标准的读入和写出。

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

    【讨论】:

    • 我发现问题了,ofs &lt;&lt; r[i].category &lt;&lt; " - " &lt;&lt;r[i].descript &lt;&lt; ' ' &lt;&lt; a*r[i].price &lt;&lt; "\n";之后我没用ofs.close();
    • @jack.dale,那么您应该回答自己的问题,并在允许时(可能需要等待一段时间)接受它作为正确答案。这样一来,您就建立了业力,并且将来其他人更容易找到解决方案。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-05-29
      • 2016-07-16
      • 2010-11-04
      • 1970-01-01
      • 2014-05-31
      • 2017-02-10
      • 2020-08-04
      • 1970-01-01
      相关资源
      最近更新 更多