C ++文件编辑,不删除其他信息

时间:2014-11-05 08:07:08

标签: c++ file stock trading

我正在编制股票交易计划。 对于此功能,我需要更改特定帐户中的余额,但每当我输出回文件时,它都会更新一个帐户的余额并删除其他所有帐户。

源代码:

void Buying()
{
    ifstream Companies;
    ifstream Account;
    Account.open("Account.txt");
    {
        int ID;
        double cash;
        string sym;
        ifstream Account;
        Account.open("Account.txt");
        //Check for errord
        if (Account.fail())
        {
            cout << "Failed" << endl;
            exit(1);
        }
        account S_accounts;
        for (int i = 0; i < 5; i++)
        {
            Account >> S_accounts.id[i] >> S_accounts.cash[i];

        }
        cout << "Please enter your last 4 digits of your ID: ";
        do
        {
            cin >> ID;
            cout << endl;
            switch (ID)
            {
            case 1111:
                cash = S_accounts.cash[0];
                break;
            case 2222:
                cash = S_accounts.cash[1];
                break;
            case 3333:
                cash = S_accounts.cash[2];
                break;
            case 4444:
                cash = S_accounts.cash[3];
                break;
            case 5555:
                cash = S_accounts.cash[4];
                break;
            default:
                cout << "ID number is invalid. please try again." << endl;
            }
            break;
        } while (ID != 1111 || ID != 2222 || ID != 3333 || ID != 4444 || ID != 5555); 
        if (cash > 36.80)
        {
            cout << "You have $" << cash << " what stock would you like to purchase?" << endl;
            listing();
            cout << endl;
            cout << "Type the stock symbol of the company that you would like to purchase." << endl;
            cin >> sym;
            double price;
            int number_of_stock;
            double total;
            int i = 0;
            while (i < 7)
            {
                Stock S_companies;
                Companies >> S_companies.price[i];
                i++;
            }
            //Stock S_companies;
            if (sym == "AAPL" || sym== "aapl")
            {
                price = 450.00;
            }
            else if (sym == "BA" || sym == "ba")
            {
                price = 75.50;
            }
            else if (sym == "INTC" || sym == "intc")
            {
                price = 22.30;
            }
            else if (sym == "RMBS" || sym == "rmbs")
            {
                price = 5.55;
            }
            if (sym == "SIRI" || sym == "siri")
            {
                price = 3.15;
            }
            if (sym == "SWKS" || sym == "swks")
            {
                price = 25.35;
            }
            if (sym == "XLNX" || sym == "xlnx")
            {
                price = 36.80;
            }
            cout << "How many stock(s) would you like to buy?" << endl;
            cin >> number_of_stock;
            total = number_of_stock*price;
            if (cash > total)
            {
                account S_account;
                ofstream Oaccount;
                Oaccount.open("Account.txt");
                if (Oaccount.fail())
                {
                    "Failed to open the file.";
                }

我知道这是我需要编辑的地方,但我绝对不知道该怎么做,它只会更改使用其ID登录的人的帐户余额。

                if (ID == 1111)
                {
                        Oaccount << S_accounts.id[0] << '\t' << '\t' << S_accounts.cash[0] - total;
                }
                Oaccount.close();
            }
            else
                cout << "You do not have enough money.";
        }
        else
        {
            cout << "Sorry, you do not have enough money in your account.";
            exit(1);
        }
        Account.close();
    }
}

1 个答案:

答案 0 :(得分:0)

我没有调试你的代码。但这是你更新/修改条目的方式...... 让number为您想要查找帐户的值...假设您要将其姓名更改为newname ...

while(!file.eof())
{
   if(obj.number==given_value)
   {
      file.tellg(pos);
      {
          //modify values here like..
          strcpy(obj.name,newname);
          file.seekg(pos);
          file.write((char*)&obj,sizeof(obj));
      }
   }
}