How to search a string between given two lines in a file

Hi C++ friends

I wrote the below C++ code to find the string "aaaa" that is line 12 & is between "hhhh" & "uuuu" stings

The function "searchSTRbetweentwoLines" takes 4 parameters - file name to search, starting string (firstSTR), string to search (SearchSTR) & search till string "tillSTR" after it finds "hhhh".

File "TextFile.log" contents
aaaa
bbbb
cccc
dddd
eeee
ffff
gggg
hhhh
kkkk
oooo
pppp
aaaa
xxxx
ffff
uuuu
llll
ssss
aaaa
qqqq
yyyy


C++ code. It should display "Found" like - "hhhh is found at line 12 of file - Found" from cout in main code.

Although, this code compiles, it does not work as expected.

can someone help to spot the logic/coding error?


#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>


using namespace std;

string searchSTRbetweentwoLines(string filename, string firstSTR, string SearchSTR, string tillSTR);

int main ()
{

cout<<"aaaa is found at line 12 of file - "<<searchSTRbetweentwoLines("TextFile.log", "hhhh", "aaaa", "uuuu")<<endl;

return 0;
}


string searchSTRbetweentwoLines(string filename, string firstSTR, string SearchSTR, string tillSTR)
{
unsigned long offset;
unsigned long offset1;
unsigned long offset2;
string line;
string line1;
string str;
ifstream fileToSearch;

fileToSearch.open (filename.c_str());
if(fileToSearch)
{
while(!fileToSearch.eof())
{
getline(fileToSearch,line);
if ((offset = line.find(firstSTR, 0)) != string::npos)
{
do
{
getline(fileToSearch,line1);
if ((offset1 = line1.find(SearchSTR, 0)) != string::npos)
{
str="Found";
}
}while ((offset2 = line.find(tillSTR, 0)) != string::npos);

}
}
}
fileToSearch.close();
return(str);
}


Mathew
Last edited on
A few issues:
1) Please post code in \[code\]\[/code\] tags - it makes reading it much much easier

2) Do you need to call 'close()' on fileToSearch when "if(fileToSearch)" evaluates to false? If not, you may consider moving the call to close()

3) The problem with the app is the search for 'tillSTR' - you are looking in "line" instead of "line1". Also, with that fix, you are continuing that loop until line1 doesn't have tillSTR; you want the oppisite, to continue until it DOES find tillSTR. Change the "!=" to "==". Should be good after that.
Hi Rollie,

Thank you for your quick reply. Yes, I got it to work.

Your 3rd suggestion above is the problem in my code above. I also thought of adding "break;" just after the line "}while ((offset2 = line1.find(tillSTR, 0)) == string::npos);" to quit the main while loop. I think this is also a good one & if not, please correct me.

Now below is my final working code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>


using namespace std;

string searchSTRbetweentwoLines(string filename, string firstSTR, string SearchSTR, string tillSTR);

int main ()
{

cout<<"aaaa is found at line 12 of file - "<<searchSTRbetweentwoLines("TextFile.log", "hhhh", "aaaa", "uuuu")<<endl;

return 0;
}


string searchSTRbetweentwoLines(string filename, string firstSTR, string SearchSTR, string tillSTR)
{
unsigned long offset;
unsigned long offset1;
unsigned long offset2;
string line;
string line1;
string str;
ifstream fileToSearch;

fileToSearch.open (filename.c_str());
if(fileToSearch)
 {
	while(!fileToSearch.eof())
	{
	  getline(fileToSearch,line);
	  if ((offset = line.find(firstSTR, 0)) != string::npos)
		{
		  do
		  {
			getline(fileToSearch,line1);
			if ((offset1 = line1.find(SearchSTR, 0)) != string::npos)
			{
			str="Found";
			}
		  }while ((offset2 = line1.find(tillSTR, 0)) == string::npos);	
		  break;
		}					
	}
 }	
return(str);

}


Mathew
Looks good! You do still want to remember to close your fileToSearch handle inside the if(fileToSearch) block, though ifstream may automatically close it in its destructor (can't recall if this is the case or not).
Hi Rollie,

Thanks again. I see what you mean to close the file. I moved it to be like below, just before "if(fileToSearch)" block exists to close it.

1
2
3
4
5
6
7
8

.
.
fileToSearch.close();
}	
return(str);

}


Mathew
ifstream may automatically close it in its destructor (can't recall if this is the case or not).


It does.
Topic archived. No new replies allowed.