Hi Guys,
Am trying to edit a file in C++. Am using TurboC++ (and have to stick to it only :P ).

I have to functions, find_detail() and balance_update().

What am trying to do is find the position of balance in the file, store it in a int variable, then in balance_update function, am going to that position using seekg() and replacing the old balance with new balance.

The problem is that the old balance is not replaced and the output is given in the end of file...
Can anyone please help me fix this? is it a prblm of bal_n and bal_s variables or something else? :-/

void find_detail()
{
 ifstream fopen(current_user, ios::in);

 fopen>>sw.balance;
 bal_l=fopen.tellg();
 bal_s=ceil(log10(sw.balance));

 fopen>>sw.age;
 age_l=fopen.tellg();
 age_s=ceil(log10(sw.age));  //find numbr of digits
 fopen.close();
}
void balance_update()
{
 float bal=0.0;
 ofstream filebalance(current_user, ios::app);
 find_detail();
 filebalance.seekp(bal_l-bal_s);
 filebalance<<bal;
 filebalance.close();
}

This problem has been driving me crazy...i just cant find the correct solution.

PS: i have to stick to Turbo C++ and have to use the same algo, means seekp-tellg thingy....

Please helpppppp

Perhaps read up on ios::app means append

Also, you open a file for output, then try to open it for input (does that even work?)

well...i tried using ios::ate...its wrks but still the pointers are going a bit crazy...they often go to the wrng position

> filebalance.seekp(bal_l-bal_s);
Just bal_l is where it started.

Also, if the old balance was say 123456 and the new balance is only 42, then what are you going to do with the other 4 chars?

Or worse, you start with 42 and you need to expand the file to write 123456.

Also, if the old balance was say 123456 and the new balance is only 42, then what are you going to do with the other 4 chars?

for that am temp using:

for(i=1;i<bal_s;i++)
 filebalance<" ";

this basically removes the extra stuff but as u said:

Or worse, you start with 42 and you need to expand the file to write 123456.

thats is exactly where every single thing gets screwed with me...that is wht pointers r pointing to wrng position...

can ne1 plz help me through this prblm??

hey...i got a fix on that prblm.... :) thx

is thr a way to make an array size increase on runtime? like a linked list? can i get any sample code of linked list ?

duh...i got it done...thx for the help @Salem (if thr was ne :P )

Hey,

i have to also change a particular string in one line of the file. can i have a look at your code ?

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.