// COM 1100, Fall 2000, Prof. Futrelle // Find the string between two delimiters // Similar to the Lab 7 tasks. // No tests for find failure done, to keep the code short. // No real program should omit the tests. // File ffind.cpp, 20 Nov 2000. #include #include using namespace std; istream & getline( istream & in, string & str, char delim ) { char ch; str = ""; // empty string, will build one char at-a-time while( in.get( ch ) && ch != delim ) str += ch; return in; } istream & getline( istream & in, string & str ) { return getline( in, str, '\n' ); } void main() { string inString, subString; ifstream in; int sStart, sFinish; in.open("f1.txt"); while(getline(in,inString)) { sStart = inString.find("#S"); sFinish = inString.find("#F"); // now the messy line, to pluck out only what's wanted: subString.assign( inString, sStart + 2, sFinish - sStart - 2 ); // vertical bars precisely show limits of string found cout << "Substring found is: |" << subString << '|' << endl; } in.close(); } /* Here's the file f1.txt with Start and Finish tags: the #Sfirst line of#F the file and #Sthe second#F #Sand there's more #Fafter that the #Send#F. */ /* Here's the output of the program above: Substring found is: |first line of| Substring found is: |the second| Substring found is: |and there's more | Substring found is: |end| */