2012-08-27 72 views
0

我試圖執行'搜索並全部替換'。我有2個指針來監視字符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; 
} 
+0

也有http://codereview.stackexchange.com這種問題 –

回答

2

您並未檢查strstr()未找到搜索字符串的情況。在這種情況下,它返回NULL,並且在那種情況下,while循環將不會做正確的事情。