Search string in a file & then copy the next 5 lines t 2nd file

Hi


I wrote the below code to search a string in a file (in file1.log) & once it find the line, I want to copy the next 5 lines to output.log file. I tried to the below first to get only 1 line but it did not work.

How I should modify it to capture next 5 lines to the output.log file?

Thanks for any help


#include <iostream>
#include <fstream>
#include <string>

using namespace std;


void PutstrToFile(string FileName, string outputFile, string strTosearch);

int main ()
{

PutstrToFile("file1.log", "output.log", "Hello, there");

return 0;
}


void PutstrToFile(string FileName, string outputFile, string strTosearch)
{
string line;
int offset;
int n;

ofstream PutFile;
PutFile.open(outputFile.c_str(), ios::app);

ifstream fileToSearch;
fileToSearch.open (FileName.c_str());
if(fileToSearch)
{
while(!fileToSearch.eof())
{
getline(fileToSearch, line);
if ((offset=line.find(strTosearch, 0)) != string::npos)
{
PutFile <<line<<endl;
}
}
}
fileToSearch.close();
PutFile.close();
}


file1.log is as below;

wwww
rrrr
tttt
Hello, there
aaaa
bbbb
cccc
dddd
eeee
ffff
gggg
hhhh


Mathew
Topic archived. No new replies allowed.