Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7945215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:47:38+00:00 2026-06-04T00:47:38+00:00

After adding records in the relative file , I am trying to update one

  • 0

After adding records in the relative file, I am trying to update one field (the balance) of a given record(client) that the user provide the account number. The update happens in the file, but it is not properly done. The output shows that the update has affected other data, and it comes also with garbage. I cannot figure out the cause of the problem. Your help will be appreciated. I am using Dev-C++. The code followed by the output is below.

#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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:47:39+00:00Added an answer on June 4, 2026 at 12:47 am

    Ha ha ha, we must be in the same class…

    You use seekg() to position pointer to read and seekp() to position pointer to write.

    Like this as you did earlier in your file:

    iof.seekp((c.account-1) * sizeof(client)); // position the pointer
    iof.write((char *)&c, sizeof(c));

    seekg = seek to get
    seekp = seek to put – requ

    good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After adding the /TSAWARE linker flag to one of my projects (Visual Studio 6),
I am getting this error after adding the libxml2.2.dylib file Linking /Users/Biranchi/Desktop/Funmovies TabBarController/build/Debug-iphonesimulator/funmovies.app/funmovies (1
I am trying to create system configuration for my module.After adding system.xml I'm getting
Does anyone know what the esiest way to update the entity model after adding/deleting
I am added a record in my table e.g. Orders . After adding the
I'm adding a User Control for each record pulled up in a data reader,
After adding the key value pairs in NSMutableDictionary, when i retrive the key/values from
After adding these settings in web.config for increasing session timeout: <sessionState mode=InProc stateConnectionString=tcpip=127.0.0.1:42424 stateNetworkTimeout=240
After adding elements to page using something like this, $('#blah').append('<div id=bugsbunny></div>'); how can I
After adding <item>170,000</items> to string.xml . It becomes so slow in building workspace, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.