我正在寻找一种方法来使用C++替换我的html文件中的某个字符串(而不是整行)。举例来说,如果我有一个HTML文件,其中包含:使用C++替换文本文件中的单词
</span><br><span class=text>morning<br></span></td>
而且我希望它编辑为:
</span><br><span class=text>night<br></span></td>
我需要通过“空中飞人”,以取代“早晨”,这是我的代码:
string strReplace = "morning";
string strNew = "night";
ifstream filein("old_file.html");
ofstream fileout("new_file.html");
string strTemp;
while(filein >> strTemp)
{
if(strTemp == strReplace){
strTemp = strNew;
}
strTemp += "\n";
fileout << strTemp;
}
此代码没拿我的文件有任何影响,我想原因是,它是唯一能够改变整条生产线,而不是局部的字符串。有人能给我一些建议来做正确的实施吗?先谢谢你。
filein >>读入令牌。令牌被空白消除。尝试'class = text> morning
'。 –
尝试使用http://www.cplusplus.com/reference/string/string/getline/逐行读取文件,然后使用http://www.cplusplus.com/reference/string/string/find/查找子字符串如果你发现它,请用http://www.cplusplus.com/reference/string/string/replace/ – Marandil