相対ファイルにレコードを追加した後、ユーザーがアカウント番号を提供する特定のレコード(クライアント)の1つのフィールド(残高)を更新しようとしています。更新はファイルで行われますが、正しく実行されていません。出力は、更新が他のデータに影響を与えたことを示しており、ガベージも含まれています。問題の原因がわかりません。あなたの助けをいただければ幸いです。Dev-C++を使用しています。コードとそれに続く出力は以下のとおりです。
#include <iostream> // cin, cout
#include <iomanip>
#include <fstream>
#include <conio.h>
using namespace std;
#define SIZE 10
struct client // Client record
{ int account; // from 1 to SIZE
char name[20];
double balance;
};
void show_file(char filename[]) // Sequential display of all records
{
client c;
int n=0;
void *ptr;
ifstream IS(filename, ios::in); // Open for sequential read
if(!IS) {cerr << filename<< " file open error." << endl; exit(1);}
cout << "\n\nSHOW_FILE: The contents of file " << filename;
while(ptr=IS.read((char *)&c, sizeof(c)))
{
cout <<'\n'<< setw(3)<< ++n << setw(6) << c.account <<setw(20)
<< c.name << setw(10) << c.balance;
}
IS.close();
}
int main(void)
{ client c;
void *ptr;
int n=0, acc,number_of_records=SIZE, field1;
double new_balance, field3;
char *fname = "credit.dat"; char field2;
cout << "\nMAKE_FILE: Creating a blank relative file " << fname
<< " containing " << number_of_records << " records.";
fstream iof(fname, ios:: in | ios::out | ios::binary );
if(!iof) {cerr << "File open error." << endl; exit(1);}
client blank={0, "", 0.0}; // Create an empty client record
while(number_of_records--)
iof.write((char *)&blank, sizeof(blank));
cout << "\n\n\nFile has been succesfully created!"; //file is still empty, no records yet.
cout<<"\n\nenter the 10 customers into the file: "<< fname<<endl<<endl;
cout << "\nAccount[1.." << SIZE
<< "], Name, Balance (0 0 0 to exit)= ";
cin >> c.account >> c.name >> c.balance;
while(0 < c.account) // && c.account <= maxrec)
{
iof.seekp((c.account-1) * sizeof(client)); // position the pointer
iof.write((char *)&c, sizeof(c));
cout << "Account[1.."<< SIZE
<< "], Name, Balance (0 0 0 to exit)= ";
cin >> c.account >> c.name >> c.balance;
}
cout << "\n\nAccount number to apply changes on balance(0 to exit) = ";
cin >> acc;
/// while(0 < acc && acc <= SIZE)
if (0<acc && acc <= SIZE)
{
//cout << "\nPositioning at " << (acc-1) * sizeof(client)<< endl;
iof.seekg((acc-1) * sizeof(client)); // position the pointer
iof.read((char *)&c, sizeof(c));
if(c.account)
cout <<'\n'<< setw(6) << c.account <<setw(20)
<< c.name << setw(10) << c.balance;
new_balance=c.balance+0.05*(c.balance); //calculation of the new balance by adding interests of 5%
cout<<"\n\n\nnew balance after the 5% interest:"<<new_balance<<endl;
c.balance=new_balance;
cout<<"current new balance: "<<c.balance<<endl; //just to check if it will be displayed
//WHERE THE PROBLEM IS...
iof.seekg(0, ios::cur); //trying to stay in the current position to apply
//change on current balance
iof<<c.account << c.name << c.balance; //trying to update record with new balance
}
else cout << "\nEmpty record";
iof.close();
cout<<"\n\nFILE after THE UPDATE: "<<endl;
show_file (fname);
cout << "\n\n";
system("pause");
return 0;
}
*********************output**************************
MAKE_FILE: Creating a blank relative file credit.dat containing 10 records.
File has been succesfully created!
enter the 10 customers into the file: credit.dat
Account[1..10], Name, Balance (0 0 0 to exit)= 1 aaaa 2399
Account[1..10], Name, Balance (0 0 0 to exit)= 2 bbbb 4000
Account[1..10], Name, Balance (0 0 0 to exit)= 3 cccc 50
Account[1..10], Name, Balance (0 0 0 to exit)= 4 dddd 5000
Account[1..10], Name, Balance (0 0 0 to exit)= 5 eeee 180
Account[1..10], Name, Balance (0 0 0 to exit)= 0 0 0
Account number to apply changes on balance(0 to exit) = 3
3 cccc 50
new balance after the 5% interest:52.5
current new balance: 52.5
FILE after THE UPDATE:
SHOW_FILE: The contents of file credit.dat
1 1 aaaa 2399
2 2 bbbb 4000
3 3 cccc 50
41667457843 c52.5♫' 5000
5 5 eeee 180
6 0 0
7 0 0
8 0 0
9 0 0
10 0 0
Press any key to continue . . .