home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tybc4 / cstring2.cpp < prev    next >
C/C++ Source or Header  |  1994-05-26  |  3KB  |  94 lines

  1.  
  2.     #include <fstream.h>
  3.     #include <cstring.h>
  4.  
  5.     const short True = 1;
  6.     const short False = 0;
  7.  
  8.     string    toFind;
  9.     string    replaceWith;
  10.     char      lineBuf[81];
  11.     ifstream  inFile;
  12.     ofstream  outFile;
  13.  
  14.     short GetFindReplace()
  15.       {
  16.       char caseFlag;
  17.  
  18.       cout << "Enter the word to find: ";
  19.       cin >> toFind;
  20.  
  21.       cout << endl << "Enter replacement word: ";
  22.       cin >> replaceWith;
  23.  
  24.       while (1)
  25.         {
  26.         cout  << "Case sensitive [Y/N]? ";
  27.         cin >> caseFlag;
  28.         caseFlag = toupper(caseFlag);
  29.  
  30.         if ((caseFlag == 'Y') || (caseFlag == 'N'))
  31.           {
  32.           string::set_case_sensitive(caseFlag == 'Y');
  33.           break;
  34.           }
  35.         }
  36.  
  37.       return (toFind != replaceWith) ? True : False;
  38.       } // end GetFindReplace()
  39.  
  40.     short ProcessFile()
  41.       {
  42.       short  findLen;      // Number of characters to find
  43.       size_t foundPos;     // Position in string where found
  44.       string buffer;       // Holds a line of input data from the file
  45.       short  startPos;     // Position in string to start search
  46.       short  replaced;     // Set to True if replacement has been done
  47.       short  numFound = 0; // Number of replacements that were made
  48.  
  49.       inFile.open("\\BC4\\README.TXT");
  50.       outFile.open("README.NEW");
  51.       findLen = toFind.length();     // Get # char's in string to find
  52.       buffer.skip_whitespace(False); // Don't skip leading spaces
  53.  
  54.       while (inFile)                 // While data left in input file
  55.         {
  56.         getline(inFile, buffer);     //   Read one line
  57.         replaced = False;            //   Init flag and position
  58.         startPos = 0;
  59.  
  60.         do
  61.           {
  62.           foundPos = buffer.find(toFind, startPos);
  63.  
  64.           if (foundPos != NPOS)      // If a match is found
  65.             {
  66.             buffer.replace(foundPos, findLen, replaceWith);
  67.             ++numFound;
  68.             replaced = True;
  69.             startPos = foundPos + 1; // Get next search start pos
  70.             }
  71.           } while (foundPos != NPOS);
  72.  
  73.         outFile << buffer << endl;   // Copy line to the output file
  74.  
  75.         if (replaced)
  76.           cout << buffer << endl;    // Show modified lines on screen
  77.         }
  78.  
  79.       inFile.close();
  80.       outFile.close();
  81.       cout << endl;
  82.       return numFound;
  83.       } // end ProcessFile()
  84.  
  85.     int main()
  86.       {
  87.       if (GetFindReplace())
  88.         cout << ProcessFile() << " words were replaced\n";
  89.       else
  90.         cout << "Error: Find and Replace words are the same\n";
  91.  
  92.       return 0;
  93.       }
  94.