2012-6-21 10:37
//获取文件大小后,申请缓冲区后读取文件内容到缓冲区,然后使用ReplaceSubStr替换缓冲区中的指定字符,最后把缓冲区的内容写入文件即可
/*获取文件大小*/
FILE *pf = fopen(szFilter, "rb");
fseek(pf, 0, SEEK_END);
int iFileSize = ftell(pf);
char *pBuffer = new char[iFileSize+1];
memset(pBuffer, 0, iFileSize+1);
fseek(pf, 0, SEEK_SET);
/*替换指定字符串*/
BOOL ReplaceSubStr(char *pSource, const char *pOldStr, const char *pNewStr)
{
if (pSource==NULL
||pOldStr==NULL
||pNewStr==NULL)
{
return FALSE;
}
std::string newStr = pNewStr;
std::string oldStr = pOldStr;
std::size_t newStrLen = newStr.size();
std::size_t oldStrLen = oldStr.size();
std::size_t pos = 0;
std::string tempStr = pSource;
std::string::size_type newStrnewStrLen = newStr.length();
std::string::size_type oldStroldStrLen = oldStr.length();
while(true)
{
pos = tempStr.find(oldStr, pos);
if (pos == std::string::npos) break;
tempStr.replace(pos, oldStrLen, newStr);
pos += newStrLen;
}
strcpy(pSource, tempStr.c_str());
return TRUE;
}