iT邦幫忙

0

請問fstream怎麼存檔

  • 分享至 

  • xImage
#include <iostream>
#include <string>
#include <string.h>
#include <stdio.h>
#include <fstream>  
using namespace std;
class eCash
{
    public:
    eCash(){
    Money = 0;
}
        void store(){
            cin >> m;
            if(m < 0){
                cout << "eCash: 請輸入大餘0的金額" << endl;
            } else {
                Money = Money + m;
                cout << "eCash: 您存了" << m << "元" << endl;
            }
        }
        void pay(){
            cin >> m;
            if(m < 0){
                cout << "eCash: 請輸入大餘0的金額" << endl;
            } else {
                if(m > Money){
                    cout << "eCash: 您的錢不夠" << endl;
                } else {
                Money = Money - m;
                cout << "eCash: 您花了" << m << "元" << endl;   
            }
        }
    }
        void display(){
            cout << "eCash: 您尚有"<< Money << "元" << endl;
        }
    private:
        int Money;
        int m;
        char str[80];
};
int main(){
    eCash p1;
    int flog = 1;
    char c, str[80];
    
    cout << "===歡迎使用eCash===" << endl;
    cout << "eCash: 請輸入您的帳號: ";
    cin >> str ;
    cout << "eCash: 帳號不存在,第一次使用!\n" << endl;
cout << str <<"您好,請選擇項目:" << endl;
cout << "s: 儲值\np: 消費\nd: 查詢餘額\nq: 離開\n" << endl ;
    while(flog != 0){
        cin >> c;
            if(c == 's'){cout << "請輸入儲存金額:" << endl; 
                p1.store();
        }
if(c == 'p'){cout << "請輸入消費金額:" << endl;
                p1.pay();
        }
            if(c == 'd'){

                p1.display();
        }
            if(c == 'q'){
               cout << "eCash: 帳號登出,已存檔!\n謝謝 , Bye Bye!" << endl;
                break;
        }
    }
    
    return 0;
}

https://ithelp.ithome.com.tw/upload/images/20210901/20140903w1WNSBnsyR.jpg

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
1
微甜的酸
iT邦新手 2 級 ‧ 2021-09-05 03:12:47
最佳解答

主程式(eCash.cpp):

#include <iostream>
#include <fstream>
#include <map>
#include <string>

using namespace std;

template<class K, class V>
class eCash{
    public:map<K,V> data;

    public:void load(map<string, int> &data) {
        ifstream file;

        file.open("eCash.txt");

        if(file.is_open()) {
            while(file.good()) {
                string account;
                int money;

                file >> account;
                file >> money;

                data[account] = money;
            }
            file.close();
        }
        else {
            cout << "[!]系統文件遺失: eCash.txt" << endl;
            exit(EXIT_FAILURE);
        }
    }

    public:void refresh(map<string, int> &data) {
        ofstream file;

        file.open("eCash.txt");

        if(file.is_open()) {
            for(const auto& x : data) {
                file << x.first << " " << x.second << " ";
            }
            file.close();
        }
        else {
            cout << "[!]系統文件遺失: eCash.txt" << endl;
            exit(EXIT_FAILURE);
        }
    }
};

int main() {
    eCash<string, int> database;
    map<string, int>::iterator iter;

    string account;

    database.load(database.data);

    cout << "=== 歡迎使用eCash ===" << endl;

    while(true) {
        cout << "eCash: 請輸入你的帳號: ";
        cin >> account;

        iter = database.data.find(account);

        if(iter == database.data.end()) {
            cout << "查無資料!" << endl;
            cout << "正在建立..." << endl;
            database.data[account] = 0;
            cout << "建立完成!" << endl << endl;
        }
        else {
            cout << "帳號開啟完成!";
            int flag = 1;
            while(flag) {
                char select;
                cout << account << "您好,請選擇項目:" << endl;
                cout << "s: 儲值" << endl;
                cout << "p: 消費" << endl;
                cout << "d: 查詢餘額" << endl;
                cout << "q: 離開" << endl;
                cout << "> ";
                cin >> select;

                switch(select) {
                    int money;
                    case 's':
                        {
                            cin >> money;
                            if(money < 0)
                                cout << "eCash: 請輸入大餘0的金額" << endl << endl;
                            else {
                                database.data[account] += money;
                                cout << "eCash: 您存了" << money << "元" << endl << endl;
                            }
                            break;
                        }
                    case 'p':
                        {
                            cin >> money;
                            if(money < 0){
                                cout << "eCash: 請輸入大餘0的金額" << endl << endl;
                            } else {
                                if(money > database.data[account]){
                                    cout << "eCash: 您的錢不夠" << endl << endl;
                                }
                                else {
                                    database.data[account] -= money;
                                    cout << "eCash: 您花了" << money << "元" << endl << endl;
                                }
                            }
                            break;
                        }
                    case 'd':
                        cout << "eCash: 您尚有: " << iter->second << "元" << endl << endl;
                        break;
                    default:
                        {
                            flag = 0;
                            cout << endl;
                            break;
                        }
                }
                database.refresh(database.data);
            }
        }
    }
}

額外檔案(eCash.txt):

admin -1

附註:eCash.txt 一定得先有一筆資料。

70002 iT邦新手 5 級 ‧ 2021-09-05 21:09:03 檢舉

https://ithelp.ithome.com.tw/upload/images/20210905/20140903cnIUY5mk8f.png不好意思 還是錯的

70002 iT邦新手 5 級 ‧ 2021-09-06 11:57:12 檢舉

微甜的酸 我先把你設置為最佳解答 答對的話我幫你按讚幾個文章(包含巴哈的)

0
roy
iT邦新手 5 級 ‧ 2021-09-01 19:55:50

可以參考 std::fstream - C++ Reference 其中的用法範例

std::fstream fs("filename", std::fstream::in | std::fstream::out);

/* 對 fs 操作 */

fs.close();
70002 iT邦新手 5 級 ‧ 2021-09-02 21:55:17 檢舉

您裡面是甚麼

0
kingjamesmvp
iT邦新手 5 級 ‧ 2022-02-24 16:25:06

請問樓主大大 最後有得到解答嗎

我要發表回答

立即登入回答