0

我试图实现“搜索并替换所有”。我有 2 个指针来监视字符 ninclude #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;
}
4

1 回答 1

2

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

于 2012-08-27T04:18:05.860 回答