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".
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.
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.
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).