簡體   English   中英

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.\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;
}

當我輸入要在屏幕上顯示的菜式並寫入文件時,在屏幕上顯示菜式就可以了,但是當它將菜式寫入文件時,即使我只選擇了一個菜,也將它們都寫入了。

如果選擇第一道菜,則預期輸出到文件中:
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');

在這一點上的任何幫助。 先感謝您。

您對該文件的實際輸出顯然來自您定義的重載<<運算符:

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";
    }
}

這將包括條件中的所有代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM