943,078 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 498
  • C++ RSS
Nov 24th, 2009
0

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

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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 Nick Evan; Nov 24th, 2009 at 5:32 am. Reason: fixed codetags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mksakeesh is offline Offline
17 posts
since Aug 2008
Nov 24th, 2009
0
Re: Trouble with fstream, not able to write properly to a file to exact position as speci
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2280
Retired and Enjoying Life
Ancient Dragon is online now Online
21,935 posts
since Aug 2005
Nov 25th, 2009
0
Re: Trouble with fstream, not able to write properly to a file to exact position as speci
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).
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mksakeesh is offline Offline
17 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Trapezoidal Trap
Next Thread in C++ Forum Timeline: PC Start Up





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC