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

时间:2012-05-11 10:42:04

标签: c++ file-io relative writefile seekg

在相关文件中添加记录后,我试图更新用户提供帐号的给定记录(客户端)的一个字段(余额)。更新发生在文件中,但是没有正确完成。输出显示更新已影响其他数据,并且还带有垃圾。我无法弄清楚问题的原因。我们将不胜感激。我正在使用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 . . .

1 个答案:

答案 0 :(得分:1)

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

使用seekg()将指针定位到read和seekp()以将指针定位到write。

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

iof.seekp((c.account-1)* sizeof(client)); //定位指针 iof.write((char *)&amp; c,sizeof(c));

seekg =寻求获得 seekp =寻求投入 - requ

祝你好运!