0

I want to open file , search for string , replace the string and then in the end print from that replaced string until the end.

So far I have opened the file using

fstream file ("demo.cpp");

Used

while (getline (file,line))

but using

string.find("something")

always give me line number 0, no matter which string I put in the arguments for find()

My question is, is there any other built in function which I can use in this situation or do I have to search through all lines, manually ?

0circle
  • 55
  • 1
  • 9
  • is the string the first thing in your file? – Iosif Murariu Jun 16 '14 at 11:59
  • Nope .. thats somewhere in the middle, actually i tried giving different strings at different positions but _getline_ always gave 0 – 0circle Jun 16 '14 at 12:02
  • What do you mean by 'always give me line number 0' ? string::find will return you std::string::npos if the searched string is not in the provided string, else it returns the position in the string, i.e. the column. – Nicolas Defranoux Jun 16 '14 at 12:08

2 Answers2

2

To get a line number where the match occurs, you have to count the lines:

if (ifstream file("demo.cpp")) {
  int line_counter = 0;
  string line;
  while (getline (file,line) {
    line_counter++;
    if (line.find("something") != string::npos) {
      cout << 'Match at line ' << line_counter << endl;
    }
  }
} else {
  cerr << "couldn't open input file\n";
}
Nicolas Defranoux
  • 2,646
  • 1
  • 10
  • 13
  • I tried it your way and it is giving me _17688425285_ as line number. I think its a bit wierd number, isnt it ? since the file that I am searching in is not more then 20 lines. – 0circle Jun 16 '14 at 12:23
  • Should be `line.find...`, and separately it's always a good idea to use `if (ifstream file("demo.cpp")) ... else std::cerr << "couldn't open\n";` +1 though - general idea's sound. – Tony Delroy Jun 16 '14 at 12:39
0

You can use this replaceAll function on every line , and have your "replace in file function" built upon it.

I modified it to return a boolean if a replacement occurred :

bool replaceAll(std::string& str, const std::string& from, const std::string& to) {
    if(from.empty())
        return false;
    size_t start_pos = 0;
    bool res = false;
    while((start_pos = str.find(from, start_pos)) != std::string::npos) {
        str.replace(start_pos, from.length(), to);
        start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
        res = true;
    }
    return res;
}

Then, what is left to you is :

  1. Iterate over all the lines of the input file
  2. Find and replace every occurrence of your string on the file
  3. If a replacement occurred, write the line count
  4. Write the replaced string on another file

The ReplaceInFile method :

void replaceInFile(const std::string& srcFile, const std::string& destFile,
                   const std::string& search, const std::string& replace)
{
  std::ofstream resultFile(destFile);
  std::ifstream file(srcFile);
  std::string line;
  std::size_t lineCount = 0;
  // You might want to chech that the files are properly opened
  while(getline(file,line))
  {
    ++lineCount;
    if(replaceAll(line, search, replace))
      std::cout << "Match at line " << lineCount << " " << replace << std::endl;
    resultFile << line << std::endl;
  }
}

Example:

int main()
{
  replaceInFile("sourceFile.txt", "replaced.txt", "match", "replace");
  return 0;
}
Community
  • 1
  • 1
quantdev
  • 23,517
  • 5
  • 55
  • 88