请教:以二进制打开方式读取文件,替换字符,采用什么方法?(分不多,请大虾赐教)

来源:百度知道 编辑:UC知道 时间:2024/05/25 15:01:54
CFile file ;
if(!file.Open(m_strFileName,CFile::modeReadWrite|CFile::typeBinary,NULL))
{
AfxMessageBox("打开文件失败!") ;
return ;
}
char *pBuffer = new char[file.GetLength() + 1] ;
pBuffer[file.GetLength()] = 0 ;
file.Seek(0, CFile::begin) ;
file.Read(pBuffer,file.GetLength()) ;
file.Close() ;

char *pTemp = new char[m_strReplace.GetLength() + 1] ;
for(int k = 0 ; k < m_strReplace.GetLength() ; k ++ )
{
pTemp[k] = m_strReplace[k] ;
}
for (int j = 0 ; j < sizeof(pBuffer) -1 ; j ++)
{
if(m_strReplace[j] == m_strFind[j])
{
m_strFind[j] = m_strReplace[j] ;
}
}
file.Open(m_strFileName,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary) ;
file.Write(pBuffer,file.GetLength()) ;
file.Close() ;

不知道错在哪里?请大虾指点

有两个变量的含义不明白:m_strReplace、m_strFind
不过有一个地方需要注意,可能是语义上的差异,即:
for (int j = 0 ; j < sizeof(pBuffer) -1 ; j ++)
我想程序的本意是在读入的内容中进行查找,匹配。可是sizeof(pBuffer)返回的是4,不是给pBuffer分配内存的大小,也不是它指向的内容的字符数量。建议在前面的语句中用一个变量记录下给pBuffer分配的内存字节大小。