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
#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?