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

Dani AI

Generated

Short, practical explanation and a safe fix for future readers.

The behavior you saw came from three separate causes. Opening the file with ios::app forces every write to the end of file, so seekp() is effectively ignored (as hinted). Using formatted extraction (operator>>) then calling tellg() gives the position after the token was read, not its start, so you must subtract the actual number of characters read. Computing the character count with ceil(log10(...)) is brittle: it fails for zero, powers of ten, negative values and decimals. Finally, replacing a number with a shorter or longer one in-place is fragile — shorter replacements leave leftover characters; longer ones require shifting the remainder of the file.

If you must keep the seek/tell approach, read the balance as text so you get the exact character length, open the file for both input and output (not append), compute the token start position, then overwrite using an exact-width write (pad or truncate to the original width). For example:

std::fstream f(name, std::ios::in | std::ios::out);
std::string bal_text;
f >> bal_text;                // parse fields until you reach the balance token
auto after = f.tellg();
auto start = after - static_cast<std::streamoff>(bal_text.size());
f.seekp(start);
f << std::setw(bal_text.size()) << std::setfill(' ') << new_balance_str;

Better, more reliable approaches:

  • Use fixed-width fields for numeric values so in-place updates are safe.
  • Or read the file and write a new temporary file, substituting the updated line, then replace the original file. That handles arbitrary-length changes without fragile pointer math.

Notes: if your Turbo C++ lacks full iostream/iomanip support, parse the token into a C buffer and use strlen to compute lengths; avoid ios::app and avoid using numeric log10 tricks to count characters. This explains why your pointers were “going crazy” and points to robust alternatives.

Recommended Answers

All 7 Replies

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 (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.