Trouble with fstream, not able to write properly to a file to exact position as speci

Please support our C++ advertiser: Download Intel® Parallel Studio Eval
Reply

Join Date: Aug 2008
Posts: 15
Reputation: mksakeesh is an unknown quantity at this point 
Solved Threads: 0
mksakeesh mksakeesh is offline Offline
Newbie Poster

Trouble with fstream, not able to write properly to a file to exact position as speci

 
0
  #1
Nov 24th, 2009
Hi,
I have an text file "CD details" with content as below

1~ Cast Away~ English~ Thriller~ U~
2~ Titanic~ English~ Romance~ A~
3~ Forest Gump~ English~ Life~ U~

I am trying to add some additional data to the start of each line by using a code as below

  1. #include<iostream>
  2. #include<fstream.h>
  3. #include<string>
  4. #include<istream.h>
  5. #include<conio.h>
  6. #include <stdio.h>
  7. using namespace std;
  8.  
  9. class CD_data_search
  10. {
  11. public:
  12. void find_file(int search_option,string search_string);
  13. };
  14. void CD_data_search::find_file(int search_option,string search_string)
  15. {
  16. string s;
  17. char p;
  18. int find_count=0,find_spport=0, loop_count=0,searchVar_temp;
  19. long pntr_postg=0,pntr_postp=0;
  20. searchVar_temp = search_option;
  21. search_option--;//To appropriately choose the correct field.
  22. fstream RW_myfile("CD Details",ios::in | ios::out);
  23. if (RW_myfile.is_open())
  24. {
  25. while (getline(RW_myfile,s))
  26. {
  27. pntr_postg=RW_myfile.tellg();
  28. pntr_postp=RW_myfile.tellp();
  29. cout<<"pntr_post_G:"<<pntr_postg<<"\n";
  30. cout<<"pntr_post_P:"<<pntr_postp<<"\n";
  31. RW_myfile.seekp(pntr_postg, ios::beg);
  32. pntr_postp=RW_myfile.tellp();
  33. RW_myfile << "TEST";
  34. }//end of while loop to read the file line by line
  35.  
  36. }//End of IF loop opening the file
  37. else
  38. {
  39. cout<<"error in opening file";
  40. }
  41. RW_myfile.close();
  42. cout<<"\n"<<"Exit Search Mode\n";
  43.  
  44. }//End of find_file(int , string)
  45. int main()
  46. {
  47. int a=0;
  48. string f;
  49. cout<<"enter choose ID:-\t";
  50. cin>>a;
  51. cout<<"Enter string:-\t";
  52. cin>>f;
  53. cin.get();
  54. CD_data_search *search_D;
  55. search_D= new CD_data_search;
  56. search_D->find_file(a,f);
  57. return 0;
  58. }

But this is not working as expected, it is writing at some other position, Do any body suggest me WHY is it behaving so?
Last edited by niek_e; Nov 24th, 2009 at 5:32 am. Reason: fixed codetags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,163
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1558
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
0
  #2
Nov 24th, 2009
You can not simply insert stuff into existing lines of a text file. Instead, you have to completely rewrite the file. Open the original file for reading, open a new temp file for writing. In a loop, read each line of the original file and rewrite it with desired changes to the output file. After that is done, close both files, delete the original, and rename the temp to be the same name as the original.
My NewYear's resolution is to lose weight.

Weight Lost since 1 Jan 2010: 8.7 lbs
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 15
Reputation: mksakeesh is an unknown quantity at this point 
Solved Threads: 0
mksakeesh mksakeesh is offline Offline
Newbie Poster
 
0
  #3
Nov 25th, 2009
Originally Posted by Ancient Dragon View Post
You can not simply insert stuff into existing lines of a text file. Instead, you have to completely rewrite the file. Open the original file for reading, open a new temp file for writing. In a loop, read each line of the original file and rewrite it with desired changes to the output file. After that is done, close both files, delete the original, and rename the temp to be the same name as the original.
Are you suggesting that the above said is the exact way to edit file.
I have test this, if we can exactly point the tellp pointer to the exact location where we need to edit and then if we insert the new word then the line get edited. I had trouble while using fstream class. If I use ifstream and ofstream separately, then I am able to do it(i.e., edit the Existing file itself).
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 341 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC