/* Filename: simplesearch.cpp Author: Br. David Carlson Date: January 3, 2007 This program allows the user to search through the text file weblist for a certain string. The program prompts the user for this target string. The weblist file contains complete pathnames for various html files, one per line, like this example: /www/carlson/cs125/final.html /www/carlson/cs125/hw/homework2.html The weblist file is assumed to be located in the same directory as this simplesearch program. The weblist file can be created with the separate makelist script. The output of the program consists of all lines of weblist that contain the target string. Note, to compile this program in Linux, use: g++ simplesearch.cpp stringhelp.cpp -o simplesearch -s To run the program, put simplesearch and weblist in the same directory. Change to that directory and enter the following command: ./simplesearch */ #include "stringhelp.h" // Next line should give the name of the text file to be searched: #define DATAFILE "weblist" // Function prototype: void SearchFile(fstream & fs); int main(void) { fstream InFile; InFile.open(DATAFILE, ios::in); if (InFile.fail()) { cout << "Error: Cannot open file named " << DATAFILE << " for input" << endl; exit(1); } SearchFile(InFile); InFile.close(); return 0; } /* Given: fs Text file stream already opened for input. Task: To prompt the user to enter a string and then to carry out a sequential search of the fs file for all instances of the target string. Each matching line of the file is printed for the user. Return: Nothing. */ void SearchFile(fstream & fs) { StringType Target, Line; int TargetLength, LineLength; cout << "Enter the target string that you wish to search for in the list of html files:" << endl; TargetLength = MyGetLine(cin, Target, StrMax); cout << endl << "Matches:" << endl << endl; if (TargetLength == 0) { cout << "Error: Cannot use an empty target string" << endl; return; } LineLength = MyGetLine(fs, Line, StrMax); while (LineLength > 0) { if (SubstringPresent(Line, Target)) cout << Line << endl; LineLength = MyGetLine(fs, Line, StrMax); } }