我正在尝试编写一个程序:
基本上,就像在文本文档上按CRTL + H一样,替换字符串,除了多次! (大约70个字符串要替换)
首先,这甚至可能吗?如果是这样,那么你可以推荐的最佳方式是什么。
顺便说一下,我生成随机字符串作为替换,不用担心。
这是我到目前为止所做的:
.... CODE above here
ifstream instubFile;
ofstream outstubFile;
outstubFile.open("Temp.txt",ios::out);
instubFile.open("stub.txt",ios::in | ios::out);
instubFile.seekg(0, instubFile.end);
unsigned int m_uNumChars = instubFile.tellg();
instubFile.seekg(0,instubFile.beg);
string inBuf;
if (instubFile) // if it opened the file
{
// read each line, check for any string matches
// if string match, replace it, add line to output string
// else, add the line to output string
while(getline(instubFile,inBuf)) // read the line
{
cout << numberoflines << endl;
for (int i = 0; i < numberoflines ; i++) // compare chars in buffer with toreplace string
{
int m_iSize = inBuf.find(m_szToReplace[i]);
cout << m_iSize << endl;
if (m_iSize > 0)
{// found
inBuf.replace(m_iSize,m_szReplacement[i].length(),m_szReplacement[i]);
outstubFile << m_szReplacement[i] << endl;
}
}
}
}
else
{
cout << "Could not open stub.txt" << endl;
}
cout << inBuf << endl;
cin.get();
delete[] m_szReplacement;
delete[] m_szToReplace;
return 0;
}
/*
int spot = inBuf.find(m_szToReplace[i]);
if(spot >= 0)
{
string tmpstring = inBuf.substr(0,spot);
tmpstring += m_szReplacement[i];
tmpstring += inBuf.substr(spot+m_szToReplace[i].length(), inBuf.length());
inBuf = tmpstring;
}
*/
在检索行之前一切正常,我不确定如何去做(比较字符串)?