Solved

File parse code to search and replace a string of text.

Posted on 2015-02-20
3
239 Views
Last Modified: 2015-02-21
I am looking for some C or C++ code that would parse a text file, and replace all occurrences of a string with another string.

So if I have a text file with this text : Mary had a little lamb who's name was Mary Anne.

And I want to replace "Mary" with "Terry",  how can I do that in C++. My file might have up to 1000 lines of text in it. The end result would be Terry had a little lamb who's name was Terry Anne.
0
Comment
Question by:brgdotnet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 40622607
#include <iostream>
#include <string>
using namespace std;
string substitute(string str,string find,string replace){
  for( size_t i=0;(i = str.find(find,i))!=string::npos;i+=replace.length() ){
      str.replace(i,find.length(),replace);
  }
  return str;
}
int main ()
{
 string str;
 while( cin.good() ){
   getline(cin,str);
   cout << substitute(str,"Mary","Terry") << endl;
 }
 return 0;
}

Open in new window

0
 
LVL 34

Assisted Solution

by:sarabande
sarabande earned 250 total points
ID: 40622875
the following program would read all text of a file to memory and replace Mary by Terry. finally the same file was written again.

#include <sys/stat.h>
#include <errno.h>
#include <fstream>
#include <string>

int main(int nargs, char * szargs[])
{
       struct stat fileinfo = { 0 };
       if (nargs < 2 || stat(szargs[1], &fileinfo) != 0) return -1;  // no commandline argument or file doesn't exist
       std::ifstream ifstext(szargs[1], std::ios::binary | std::ios::in);
       std::string buf(fileinfo.st_size, '\0');
       if (!ifstext.read(&buf[0], fileinfo.st_size)) return errno;
       ifstext.close();
       size_t pos, lpos = 0;
       while ((pos = buf.find("Mary", lpos)) != std::string::npos)
       {
              buf.replace(pos, sizeof("Mary")-1, "Terry");
              lpos = pos+1;
       }
       std::ofstream ofstext(szargs[1], std::ios::binary | std::ios::out);
       if (!ofstext.write(buf.c_str(), buf.length())) return errno;
       ofstext.close();
       return 0;
}

Open in new window


you would call the program at the commmandline passing the filepath as argument.

note, the while loop is equivalent to the for loop ozo has used in the substitute function.

you may mix both codes by opening the file in text mode for read, then read line by line using std::getline from file, for each line do the substitution and add the modified line into a std::vector<std::string> container. finally close the file and open it again for write. write all the lines from vector back to the output file (note, you have to add a linefeed character to each line).

Sara
0
 
LVL 84

Expert Comment

by:ozo
ID: 40623022
Note that the while loop would not be equivalent to the for loop if you wanted to replace "Mary" with "Mary, Mary, Quite Contrary"
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question