Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 03-01-2009, 02:43 PM   #1 (permalink)
TechFreakiez.com
 
Abhishek Dwivedi's Avatar
 
Join Date: Sep 2006
Location: New Delhi
Posts: 621
Default HELP!!!: Seekp() & Tellg() Problem in C++.


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?

Code:
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();
}
Code:
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
__________________
Personal Log | Star date 05.04.2009: TDF Meet Kanpur was Awesome :D
www.TechFreakiez.com
Abhishek Dwivedi is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 03-01-2009, 04:53 PM   #2 (permalink)
Alpha Geek
 
Krazy_About_Technology's Avatar
 
Join Date: Jun 2004
Location: Noida - India
Posts: 765
Default Re: HELP!!!: Seekp() & Tellg() Problem in C++.

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.
__________________
Dell Inspiron 1525 - C2D 2 Ghz, 3GB, 250GB, X3100 :)

Samsung Omnia Pro B7610 with Stock WM 6.1 ROM

Blog: http://www.sumitbhardwaj.co.in/blog
Krazy_About_Technology is offline  
Old 03-01-2009, 06:13 PM   #3 (permalink)
TechFreakiez.com
 
Abhishek Dwivedi's Avatar
 
Join Date: Sep 2006
Location: New Delhi
Posts: 621
Default Re: HELP!!!: Seekp() & Tellg() Problem in C++.

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?
__________________
Personal Log | Star date 05.04.2009: TDF Meet Kanpur was Awesome :D
www.TechFreakiez.com

Last edited by Abhishek Dwivedi; 03-01-2009 at 06:49 PM.
Abhishek Dwivedi is offline  
Old 03-01-2009, 09:50 PM   #4 (permalink)
Alpha Geek
 
Krazy_About_Technology's Avatar
 
Join Date: Jun 2004
Location: Noida - India
Posts: 765
Default Re: HELP!!!: Seekp() & Tellg() Problem in C++.

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 :
Code:
bal_l=fopen.tellg();
it should be:

Code:
bal_l=fopen.tellg()-sizeof(sw.balance);
but for for string vars, it should be:

Code:
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
__________________
Dell Inspiron 1525 - C2D 2 Ghz, 3GB, 250GB, X3100 :)

Samsung Omnia Pro B7610 with Stock WM 6.1 ROM

Blog: http://www.sumitbhardwaj.co.in/blog
Krazy_About_Technology is offline  
Old 04-01-2009, 11:14 AM   #5 (permalink)
TechFreakiez.com
 
Abhishek Dwivedi's Avatar
 
Join Date: Sep 2006
Location: New Delhi
Posts: 621
Default Re: HELP!!!: Seekp() & Tellg() Problem in C++.

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 ()...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...
__________________
Personal Log | Star date 05.04.2009: TDF Meet Kanpur was Awesome :D
www.TechFreakiez.com

Last edited by Abhishek Dwivedi; 04-01-2009 at 01:11 PM.
Abhishek Dwivedi is offline  
Old 16-01-2009, 02:55 AM   #6 (permalink)
Legen-wait for it-dary!
 
dheeraj_kumar's Avatar
 
Join Date: Dec 2004
Location: Chennai
Posts: 2,471
Default Re: HELP!!!: Seekp() & Tellg() Problem in C++.

^^ Pointers are the only way to solve your problem. They are dynamic. Since you use c++, use the new operator. malloc() for c.
__________________
If the Start Windows Restart when Windows starts check box is checked Windows Restart will start automatically every time Windows is started. - Actual excerpt from a windows program help file
dheeraj_kumar is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem With Data Recovary Software Problem Stellar Phoenix njm Software Q&A 1 08-05-2007 12:31 PM
Problem Related To Dvd and Iso Buring , any genius here who can resolve this problem vijayendra QnA (read only) 2 14-01-2007 09:38 AM
Problem!Problem!Problem Help Me November Cd And December dvd Doesnt Work yyy??? sourav_digit QnA (read only) 3 27-12-2006 02:33 PM
Serius problem with my screen. Can u tell it s/w or hard ware problem . tusarks Hardware Q&A 7 12-06-2006 01:58 PM
fedora cd iso problem(installation from hard drive problem) harmax Open Source 3 01-07-2005 11:34 AM

 
Latest Threads
- by gforz
- by soumya
- by Sujeet
- by icebags
- by Charan

Advertisement




All times are GMT +5.5. The time now is 03:04 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2