无法更新相对文件中的记录字段,C++

cannot update a field of a record in a relative file, C++

本文关键字:记录 字段 C++ 更新 相对 文件      更新时间:2023-10-16

在相关文件中添加记录后,我正在尝试更新用户提供的帐号的给定记录(客户端)的一个字段(余额)。更新发生在文件中,但未正确完成。输出显示更新影响了其他数据,并且还附带垃圾。我无法找出问题的原因。您的帮助将不胜感激。我正在使用开发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 << "nnSHOW_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 << "nnnFile has been succesfully created!";  //file is still empty, no records yet.
   cout<<"nnenter 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 << "nnAccount 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<<"nnnnew 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<<"nnFILE after THE UPDATE: "<<endl;
  show_file (fname);

   cout << "nn"; 
   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 . . .

哈,我们必须在同一个班级...

您可以使用 seekg() 定位要读取的指针,使用 seekp() 来定位要写入的指针。

就像您之前在文件中所做的那样:

iof.seekp((c.account-1) * sizeof(client)); 放置指针iof.write((char *)&c, sizeof(c));

寻求 = 寻求获得寻求 = 寻求放置 - 要求

祝你好运!