2017-11-02 6 views
0

口座xyzの金額を引き出したいのですが、金額を引き出す際には、書かれています)私は何ができますか?私はC++の銀行システムに関するプロジェクトを持っています。金額を払い戻したいが、金額を払い戻す場合は、特定のポジションのファイルに新しい金額を更新することはできません。

これは私の引き出し機能です。ここで

void withdraw() 
{ 
    ofstream f2("bank",ios::out | ios :: app); 
    ifstream f1("bank",ios::in | ios :: app); 

    f1.seekg(0); 
    long long a_num; 
    long double w_amount; 

    cout << "Enter Account Number :" << endl; 
    cin >> a_num; 
    Bank ac; 

    while (f1 >> acc_num >> name >> acc_type >> amount){ 

     if(acc_num == a_num){ 

      int g = f1.tellg(); 
      cout << "Get" << g << endl; 
      int p=g; 
      acc_num=0; 
      f2.seekp(p,ios::beg); 
      cout << "Name   :" << name << endl; 
      cout << "Account type :" << acc_type << endl; 
      cout << "balance  :" << amount << endl; 

      cout << "Enter withdraw amount : " << endl; 
      cin >> w_amount; 
      amount = amount - w_amount; 
      cout << "Balance :" << amount << endl; 

      f2.seekp(p,ios::beg); 

      f2 << amount << endl; 
     } 

    } 
} 

が何のファイル looks like

bank.txt 
123456789xyz savings 1200 
123456789pyr current 1600 
+2

を参照してくださいナレッジの詳細については

char text [5] = "good"; // do the seekp operation. fp.write (text,5); 

。あなたはすべてを読んで、修正し、書いて – user463035818

答えて

0

問題は、ファイル名がbank.txt非常に基本的なものですが、あなたははifstreamofstreamのオブジェクトを渡しているctorちょうど銀行、これはおそらく開かれた操作が代わりにいくつかの他のファイルを開きますように動作しません

ofstream f2("bank",ios::out | ios :: app); 
ifstream f1("bank",ios::in | ios :: app); 

そして第二に、あなたが同時に同じファイルを開くことはできませんが、あなたの代わりに、あなたの質問に対処fstreamのオブジェクト

fstream fp("bank.txt",ios::in | ios::out | ios::app); 

を使用したファイルへの書き込みにfp.write()機能を使用することができ、その最初のパラメータがあります文字へのポインタ、2番目はファイルに書き込む文字の数なので、それに応じて2番目のパラメータを設定できます。

それは、このようなことがあります。あなたは、ファイル内の特定の位置を更新カントこのhttp://www.cplusplus.com/reference/ostream/ostream/write/

関連する問題