| | |
Trouble with fstream, not able to write properly to a file to exact position as speci
![]() |
•
•
Join Date: Aug 2008
Posts: 15
Reputation:
Solved Threads: 0
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
But this is not working as expected, it is writing at some other position, Do any body suggest me WHY is it behaving so?
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)
#include<iostream> #include<fstream.h> #include<string> #include<istream.h> #include<conio.h> #include <stdio.h> using namespace std; class CD_data_search { public: void find_file(int search_option,string search_string); }; void CD_data_search::find_file(int search_option,string search_string) { string s; char p; int find_count=0,find_spport=0, loop_count=0,searchVar_temp; long pntr_postg=0,pntr_postp=0; searchVar_temp = search_option; search_option--;//To appropriately choose the correct field. fstream RW_myfile("CD Details",ios::in | ios::out); if (RW_myfile.is_open()) { while (getline(RW_myfile,s)) { pntr_postg=RW_myfile.tellg(); pntr_postp=RW_myfile.tellp(); cout<<"pntr_post_G:"<<pntr_postg<<"\n"; cout<<"pntr_post_P:"<<pntr_postp<<"\n"; RW_myfile.seekp(pntr_postg, ios::beg); pntr_postp=RW_myfile.tellp(); RW_myfile << "TEST"; }//end of while loop to read the file line by line }//End of IF loop opening the file else { cout<<"error in opening file"; } RW_myfile.close(); cout<<"\n"<<"Exit Search Mode\n"; }//End of find_file(int , string) int main() { int a=0; string f; cout<<"enter choose ID:-\t"; cin>>a; cout<<"Enter string:-\t"; cin>>f; cin.get(); CD_data_search *search_D; search_D= new CD_data_search; search_D->find_file(a,f); return 0; }
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
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
Weight Lost since 1 Jan 2010: 8.7 lbs
•
•
Join Date: Aug 2008
Posts: 15
Reputation:
Solved Threads: 0
0
#3 Nov 25th, 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.
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).
![]() |
Similar Threads
- why i cant write records into file in turbo-pascal??? (Pascal and Delphi)
- Trying to write in and mp3 file (C++)
- Need to write postfix to a file (Java)
- fstream (C++)
- assignment help [fstream, on modifying a record in a file] (C++)
- Error trying to write to existing file (C)
- How to write a header file (C++)
Other Threads in the C++ Forum
- Previous Thread: Trapezoidal Trap
- Next Thread: PC Start Up
Views: 341 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays assignment beginner binary borland c++ c/c++ calculator char class classes code compile compiler console constructor conversion convert count data delete desktop dll encryption error file forms fstream function functions game givemetehcodez graph homework http iamthwee ifstream input int java lazy lib link linker list loop looping loops map math matrix memory newbie news number objects output pointer pointers problem program programming project python qt read recursion recursive reference return search sort sorting spoonfeeding string strings struct student studio system template templates text tree url variable vc++ vector video visual visualstudio win32 window windows winsock wordfrequency wxwidgets






