用指针搜索和替换

时间:2012-08-27 03:37:27

标签: c++ pointers strstr

我正在尝试实施'搜索并替换所有'。我有2个指针来监控字符nclude     #包括     #包括     使用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;
}

1 个答案:

答案 0 :(得分:2)

您没有检查strstr()找不到搜索字符串的情况。在这种情况下,它会返回NULL,而在这种情况下,你的while循环循环不会做正确的事情。