C++ program to remove certain words from a string?
-
I have an origional text file...I need to remove certain words from the file..replace them with new words and save it as a new file...its a file with a bunch of lines and some of the words appear multiple times throughout the file..this is what I have to far...right now im trying to remove the word "<head>" and replace it with ### #include<iostream> #include<cstdlib> #include<fstream> #include<string> using namespace std; int main() { string line; ifstream infile; ofstream outfile; size_t a; a=line.find_first_of("<head>"); infile.open("source.txt"); outfile.open("F.txt"); if(infile.fail()) { cout<<"File was not opened"<<endl; system("pause"); return 0; exit(1); } while(getline(infile,line)) { while(a!=string::npos) { line[a]='###'; a=line.find_first_of("<head>",a+1); outfile<<line; } } system("pause"); return 0; } I was thinking that if I removed this word "<head>" I could just make more while loops to remove all the other words. Thanks alot to anyone who helps!
-
Answer:
For this: line[a]='###'; my compiler says: warning: multi-character character constant ... and your logic is not right for replacing found words and writing to the output file. You don't need "more while loops". Here's how I would fix your code: #include <iostream> #include <fstream> #include <string> using namespace std; const char *inFileName = "source.txt"; const char *outFileName = "F.txt"; const string findStr("<head>"); const string replStr("###"); int main(int argc, char *argv[]) { string line; ifstream infile(inFileName); size_t a; if (infile.fail()) { cout << "File was not opened" << endl; return -1; } ofstream outfile(outFileName); while (getline(infile,line)) { while ((a = line.find(findStr)) != string::npos) { line.erase(a, findStr.size()); line.insert(a, replStr); } outfile << line << endl; } infile.close(); outfile.close(); return 0; }
MJ at Yahoo! Answers Visit the source
Other answers
You can use erase() function. This C++ tutorial may help you:http://www.worldbestlearningcenter.com/index_files/c++_string-manipulation-erase.htm
Sovandara
Do you really need to write a program to do this? Any text editor (even notepad) will do this for you.
Arthur
Related Q & A:
- How to split a string in C++?Best solution by Stack Overflow
- How Can I use .net dll in C program?Best solution by Stack Overflow
- How to marshall a string in C?Best solution by Stack Overflow
- How to run an executable from within a c program?Best solution by Stack Overflow
- How do I remove certain results from a Google search? Is there a way?
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.