0

Im trying to implement a 'search and replace all'. I have 2 pointers to monitor characters nclude #include #include using namespace std;

const int BUFSIZE = 256;

const char * const SEARCH = "the";
const char * const REPLACE = "Who is John";

int main()
{
    char buf[BUFSIZE];
      Error(SOURCE_FILE);

    //open file for append and update
    fstream destination(DESTINATION_FILE, 
        ios_base::in | ios_base::out | ios_base::app);

    //check if destination file is open
    if (!destination.is_open())
        Error(SOURCE_FILE);


        bufPtr += length;               //move bufPtr to point past the SEARCH

        destination << bufPtr;          //write rest of buf to destination
        //destination.seekg(0);

        //write lines from source to destination
        //if(!(destination << buf << '\n'))
            //Error(SOURCE_FILE);
    }   
    source.close();
    destination.close();

    return 0;
}
blitzeus
  • 485
  • 2
  • 10
  • 28

1 Answers1

2

You're not checking for the case where strstr() doesn't find the search string. It returns NULL in that case, and your while loop loop won't do the right thing in that case.

Barmar
  • 741,623
  • 53
  • 500
  • 612