home *** CD-ROM | disk | FTP | other *** search
-
- #include <fstream.h>
- #include <cstring.h>
-
- const short True = 1;
- const short False = 0;
-
- string toFind;
- string replaceWith;
- char lineBuf[81];
- ifstream inFile;
- ofstream outFile;
-
- short GetFindReplace()
- {
- char caseFlag;
-
- cout << "Enter the word to find: ";
- cin >> toFind;
-
- cout << endl << "Enter replacement word: ";
- cin >> replaceWith;
-
- while (1)
- {
- cout << "Case sensitive [Y/N]? ";
- cin >> caseFlag;
- caseFlag = toupper(caseFlag);
-
- if ((caseFlag == 'Y') || (caseFlag == 'N'))
- {
- string::set_case_sensitive(caseFlag == 'Y');
- break;
- }
- }
-
- return (toFind != replaceWith) ? True : False;
- } // end GetFindReplace()
-
- short ProcessFile()
- {
- short findLen; // Number of characters to find
- size_t foundPos; // Position in string where found
- string buffer; // Holds a line of input data from the file
- short startPos; // Position in string to start search
- short replaced; // Set to True if replacement has been done
- short numFound = 0; // Number of replacements that were made
-
- inFile.open("\\BC4\\README.TXT");
- outFile.open("README.NEW");
- findLen = toFind.length(); // Get # char's in string to find
- buffer.skip_whitespace(False); // Don't skip leading spaces
-
- while (inFile) // While data left in input file
- {
- getline(inFile, buffer); // Read one line
- replaced = False; // Init flag and position
- startPos = 0;
-
- do
- {
- foundPos = buffer.find(toFind, startPos);
-
- if (foundPos != NPOS) // If a match is found
- {
- buffer.replace(foundPos, findLen, replaceWith);
- ++numFound;
- replaced = True;
- startPos = foundPos + 1; // Get next search start pos
- }
- } while (foundPos != NPOS);
-
- outFile << buffer << endl; // Copy line to the output file
-
- if (replaced)
- cout << buffer << endl; // Show modified lines on screen
- }
-
- inFile.close();
- outFile.close();
- cout << endl;
- return numFound;
- } // end ProcessFile()
-
- int main()
- {
- if (GetFindReplace())
- cout << ProcessFile() << " words were replaced\n";
- else
- cout << "Error: Find and Replace words are the same\n";
-
- return 0;
- }
-