hi!
im writing some code about a banking system
i have done so far the classes and functions to create account, save date in a text file find account by last name show account info and although i can read from the file the amount of a client i cant replace that amount with the new
one
...to be more clear
void BankAccount::setamount(string lname, float amount)
{
const char NUM=30;
char buffer[NUM];
fstream in("records.txt");
/*this is my a little bit silly try to avoid binary files and make the in pointer go to the proper line of the file..and ofcourse it doesnt write anything in the file*/
while(in)
{getline(buffer,NUM);
if(buffer==lname)
for(i=0;i<8;i++)
getline(buffer,NUM);
in<<amount;
}
}

I can think of no "good" way to do this. Some options I can think of include:

1) If records.txt isn't "too" big, read it all into a container in your program, search the container for the desired location, change what you will, and write the containers content back to storage.

2) If records.txt is "big", segment the file and read each segment into your program one at a time, search it, if you don't find what you want write it back, if you do find what you want, change and then write it back OR

3) Use a (hash) function to determine the location of any given record within the file when you put it there so you just run the function to find out where to look when you want to retrieve it, change it, and write it back OR

4) Hardcode the location of each record in records.txt in a separate helper file. Read the helper file into the program and use it to locate the desired file and then retrieve it from records.txt, change it, and write it back.

I'm sure there are other protocols, too. It's your choice based on your needs and your knowledge.

This article has been dead for over six months. Start a new discussion instead.