Replacing word in a file using c++
i've been trying it since a long time but not getting the correct approach! 👀
Administrator • Nov 12, 2012
Member • Nov 12, 2012
string str = "(team)"; string strTemp; fstream file ("file.doc", ios::out|ios::app) while(!file.eof()) { if(strTemp == str) file<<strTemp; }I tried this, but the loop doesn't seem to end! there's something wrong in the conditional statement which i'm not able to figure! So I could use up some help over there! 😀 thanks!
Member • Nov 12, 2012
Member • Nov 12, 2012
oh sorry wrong code posted..jeffrey samuelHey am I seeing some thing wrong there is no data allocated to strtemp
Do check it out bro initially there is no data in strtemp and so the error is flagged
Member • Nov 12, 2012
Member • Nov 13, 2012
Vishal0203Actually, its a part of a big program.. Sorry for that...
I'll post the loop part... so, here it is..
string str = "(team)"; string strTemp; fstream file ("file.doc", ios::out|ios::app) while(!file.eof()) { if(strTemp == str) file<<strTemp; }I tried this, but the loop doesn't seem to end! there's something wrong in the conditional
statement which i'm not able to figure! So I could use up some help over there! 😀 thanks!
Member • Nov 13, 2012
Wait a sec!! there is a lot of confusion over here. Let me post the exact function block of code.grsalviFirst of all the part of code pasted did not help to understand what you are actually doing.
But checkout few things :
1] " ios::app mode "
You have opened file in output mode and append mode. In this case the file output pointer is
taken directly to end of file ,when it is opened and you are not allowed to randomly edit file,that is all you can do is add new thing to file at the end.
solution : use ios::ate mode.
ios ::ate (at end ) mode also opens file with pointer end, but you are allowed to take pointer anywhere in file and do editing.
2] Are you using same file stream to read and edit file ???
solution:
If so then once the word to be edited has been read,you have to take back your pointer back to first byte of the word to be edited using file.fseekp();
One suggestion :
It is always better while editing a file,to create two file streams .One reads file and another writes a temporary file.
void team::team_list() { string str ="(team)";//existing word string strTemp; string tname;// to be placed in file cout<<"\n\n"<<"\t\t\t"<<"Enter the team name: "; cin.sync(); getline(cin,tname,'\n'); name +=".doc"; fstream file(tname.c_str()); CopyFile("D:\\C++\\proj\\team.doc",tname.c_str(),TRUE); file.close(); ifstream second(tname.c_str(), ios::in); while(!second.eof()) { if(strTemp == str)// I'm facing problem here second<<tname; } dept_stud(); }so here it is....... in which I'm not opening the file in append mode..... I'll brief you about what my entire program actually does. The setup consist of a main .exe file and two pre made documents documents..
Member • Nov 13, 2012
void team::team_list()This code should have given compile time error. May be you pasted wrong code again.
{
string str ="(team)";//existing word
string strTemp;
string tname;// to be placed in file
cout<<"\n\n"<<"\t\t\t"<<"Enter the team name: ";
cin.sync();
getline(cin,tname,'\n');
name +=".doc";
fstream file(tname.c_str());
CopyFile("D:\\C++\\proj\\team.doc",tname.c_str(),TRUE);
file.close();
ifstream second(tname.c_str(), ios::in); / file opened for read purpose
while(!second.eof())
{
if(strTemp == str)// I'm facing problem here
second<<tname; // using second for output ???}
dept_stud();
}
void Category::replace() { cout<<"Enter Category Name to be replaced in file : "; cin>>catName; string newCat; cout<<"Enter New Category : "; cin>>newCat; // new category called newCat ifstream fin; ofstream fout; fileIn(fin,catFile); // function to open file in read mode ,fin assocaited with existing file of // of categories string catFileTemp; // temp file created catFileTemp=catFile; int x=catFileTemp.find("\."); catFileTemp.insert(x,"temp"); fileOut(fout,catFileTemp); // uisng function fileOut : fout stream assocaited with temp file string cat; while(fin>>cat) // reading exiting file and writing to temp file { if(cat==catName) // when word to be replaced is detected, the new // word is inserted in temp file. { cat=newcat; } cat.append("\n",2); fout<<cat; } fin.close(); fout.close(); fileCopyStr(catFileTemp,catFile);// user defined function which copies temp file to old file and // deletes temp file }Hope it helps.All the best . 👍
Member • Nov 13, 2012
void rem(string &initialString, string whatToRemove){ size_t pos=initialString.find(whatToRemove); if (pos!=string::npos) initialString.erase(initialString.begin()+pos, initialString.begin()+pos+whatToRemove.length()); }But i couldn't understand it much... That's the reason I asked the question here! 👀
Member • Nov 13, 2012
I checked the link.Its like i said,they are using it to deal with string objects.Vishal0203but i came across many codes around the web, in which people have used find and replace.. 😔 and the code is just about 5 - 6 lines (too less)!!
Have a look at this... this snippet is in daniweb
void rem(string &initialString, string whatToRemove){ size_t pos=initialString.find(whatToRemove); if (pos!=string::npos) initialString.erase(initialString.begin()+pos, initialString.begin()+pos+whatToRemove.length()); }But i couldn't understand it much... That's the reason I asked the question here! 👀
#-Link-Snipped-#
Heres the link of that!!
Member • Nov 14, 2012
void Category::replace() { cout<<"Enter Category Name to be replaced in file : "; cin>>catName; string newCat; cout<<"Enter New Category : "; cin>>newCat; ifstream fin; ofstream fout; fileIn(fin,catFile); string catFileTemp; //temp file created ... (Is this a temp file created or string??) catFileTemp=catFile; int x=catFileTemp.find("\."); catFileTemp.insert(x,"temp"); fileOut(fout,catFileTemp); string cat; while(fin>>cat) { if(cat==catName) { cat=newcat; } cat.append("\n",2); // What does this line do?? fout<<cat; }I've re commented the code, check it once
Member • Nov 14, 2012
Member • Nov 14, 2012
No problem buddy.Cpp is my favorite like yours.Vishal0203Thanks a lot...
So I'll stick to your code here.. I do have some doubts in it!!
void Category::replace() { cout<<"Enter Category Name to be replaced in file : "; cin>>catName; string newCat; cout<<"Enter New Category : "; cin>>newCat; ifstream fin; ofstream fout; fileIn(fin,catFile); string catFileTemp; //temp file created ... (Is this a temp file created or string??) catFileTemp=catFile; int x=catFileTemp.find("\."); catFileTemp.insert(x,"temp"); fileOut(fout,catFileTemp); string cat; while(fin>>cat) { if(cat==catName) { cat=newcat; } cat.append("\n",2); // What does this line do?? fout<<cat; }I've re commented the code, check it once
remaining things are fine!!
But, I'm sorry I'm gonna use you a li'l more.. Hope you don't mind!! 😀
I have another problem over here.. dealing with the same thing though.. But i just want to know can we perform the same thing in this kind of file??
void Category::replace() { cout<<"Enter Category Name to be replaced in file : "; cin>>catName; string newCat; cout<<"Enter New Category : "; cin>>newCat; ifstream fin; ofstream fout; fileIn(fin,catFile); string catFileTemp; [COLOR=#ff0000]//temp file created ... (Is this a temp file created or string?[/COLOR] */ans : You are correct a string is created.This string contains name of temp file. There is a function which i used below like fileIn(catFile,fin); This user defined function takes trouble to convert string catFile to char array and open file using fin.open(catFileArray,ios::in); Why did extra work of creating string then array and then file opening ?? This you very well know as strings provide more flexibility such as find() and insert() then arrays. */ catFileTemp=catFile; int x=catFileTemp.find("\."); catFileTemp.insert(x,"temp"); fileOut(fout,catFileTemp); string cat; while(fin>>cat) { if(cat==catName) { cat=newcat; } cat.append("\n",2); // What does this line do?? fout<<cat; } /* Ans: cat.append("abc",3) : it adds 3 characters to end of string. For example : if cat is "123"; after cat.append("xyz",3); cat becomes "123xyz". Now why "\n" was appended is some thing required by output of program.It was desired that the text file should show one category in one line(That is '\n' is between two categories). You may omit it ,if not required. */
Member • Nov 14, 2012
Member • Nov 14, 2012
Member • Nov 14, 2012
grsalvi2] Now will it work for the certificate shown above ???
Ans :
You said that the file (having certificate details ) is already on drive .
Right ?
Can you tell in which form is it .docx or .txt .
Member • Nov 14, 2012
grsalvi1] fileIn() and fileOut() functions are functions defined by
2] int x=catFileTemp.find("\."); catFileTemp.insert(x,"temp");
x gets location at of '.' in string catFileTemp.
Purpose is you copies catFile to catTempFile.Now you are going to create another
file using catTempFile(that is temp).
So to add temp in file name i.e. to get file name " documentTemp.txt "
above two statements are used.
Member • Nov 14, 2012
My program actually wrote,read and allowed editing in ANSI format for .txt files.Vishal0203it is in .doc format as the .docx doesn't support manipulation... (bloody microsoft people)
Member • Nov 14, 2012
There are just user defined functions.Vishal0203Are functions defined by?? I think you didn't complete the sentence...
Member • Nov 14, 2012
i get problems editing this format... I surfed the web for this and found that there is a different way to manipulate this format... And it can be done only in Microsoft Visual C++ 👀grsalviMy program actually wrote,read and allowed editing in ANSI format for .txt files.
If the .doc is written in same format ,it will work definitely.
Member • Nov 14, 2012
Yes,even i doubt if .doc will work.Vishal0203i get problems editing this format... I surfed the web for this and found that there is a different way to manipulate this format... And it can be done only in Microsoft Visual C++ 👀
Member • Nov 14, 2012
nope.. I don't think it'll work... it'll not even seek the file.. I tried this long back.. the c++ codes cannot manipulate the borders fonts etc. 👀grsalviYes,even i doubt if .doc will work.
Do you think copying from .doc the content and pasting it to .txt file and then working on .txt file using your program ,will work???
Because .doc add lots of thing according to Microsoft standards which is not present in a .txt file.
Member • Nov 14, 2012
Exactly,its beyond c++ scope.C++ works on plain text format.Vishal0203nope.. I don't think it'll work... it'll not even seek the file.. I tried this long back.. the c++ codes cannot manipulate the borders fonts etc. 👀
Member • Nov 14, 2012
#-Link-Snipped-#grsalviExactly,its beyond c++ scope.C++ works on plain text format.
so if you write .doc as simple text file while borders and other page designers are not omitted ,it is possible.
Find out ,what you can do and Post here ,let me know.
Member • Nov 14, 2012
Exactly your situation but language used is c#.Vishal0203#-Link-Snipped-#
here is something which says that we can manipulate using visual c++
looks a li'l complicated though!
Member • Nov 14, 2012
No idea about c# and java now.. that's why I'm programming in c++ 😉grsalviExactly your situation but language used is c#.
<a href="https://www.codeproject.com/Articles/36432/Edit-Microsoft-Word-Document-From-Your-Application" target="_blank" rel="nofollow noopener noreferrer">Edit Microsoft Word Document From Your Application - CodeProject</a>