# HELP!!!: Seekp() & Tellg() Problem in C++.



## Abhishek Dwivedi (Jan 3, 2009)

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

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();
}
```

thx in advance


----------



## Krazy_About_Technology (Jan 3, 2009)

open the file in ios::ate in balance_update function, seekp will work then. ios::app mode does not provides a random access seek pointer. You will always end up appending data coz obviously thats what the append mode is for.


----------



## Abhishek Dwivedi (Jan 3, 2009)

yeah..i did that... ios::ate got it wrking... but still, the pointers r going crazy...pointing wrong places...

and anothr problem, if am trying to replace  "ABC" with "ABCD", it effects the next line...as "ABC" is 3 char and "ABCD" is 4 char long and so whn i delete the line where "ABC" is placed, i have to make space for 4 char to place "ABCD"...how do i do that?


----------



## Krazy_About_Technology (Jan 3, 2009)

Dude you are calculating the pointer position wrong. When you write to a file, both the pointers move along. So after writing to file, calculate the no of characters written and then subtract that from the position where the pointers are at after writing. I mean after writing balance into file, instead of :

```
bal_l=fopen.tellg();
```

it should be:


```
bal_l=fopen.tellg()-sizeof(sw.balance);
```

*but for for string vars, it should be:*


```
bal_l=fopen.tellg()-len(str);
```


Actually the second thing you said is not possible as you want it to. The programs that allow inserting of items in their files make a separate copy of that file in memory or in a temporary files(like ms word). Then when the changes are finalized the original file is re-written by merging the contents of both.

So for your program the best strategy would be to make a data structure that can hold all the variables written in the file, since you are storing limited data of particular types in your file. On startup, read these settings and store them in vars then at the time of exiting, write them back to the file by opening the file in ios:ut mode (effectively erasing the previous contents). This way you will have a temporary in-memory representation of your file while the program is running and a permanent copy of the changes will be saved to disk on exiting. This works perfectly well for program which edit small files. 

MS Word uses the second approach as it is meant for editing files of all sizes.

Tell me if there are any more doubts  i'll be happy to help even though i am not at all concerned with Vodafone


----------



## Abhishek Dwivedi (Jan 4, 2009)

lolz....okey...i'll give it a try n c if it wrks


*UPDATE*: okey @Krazy_About_Technology, things wrking now...i had to use the "put-in-stucture-edit-putback" algo  as u and gaurav(in the othr thread) said...

the only prblm is with the other file...the MotherFile P)...that file holds username n passwrd of each user in this format :  username+password  (each line as this format)
the prblm is whn a user changes his password, how do i update it here....
i have something in mind but open to suggestions

*UPDATE AGAIN:* any way i can make a char array size increase dynamically? means it increases in runtime?...i knw i can use linked list but i need a small code for this...
just need a char array to increase during runtime...


----------



## dheeraj_kumar (Jan 16, 2009)

^^ Pointers are the only way to solve your problem. They are dynamic. Since you use c++, use the new operator. malloc() for c.


----------

