我想编辑一个文件,找到其中的一个字符串将其替换,比如将“192.168.88.7”替换为“192.168.88.142”(根据输入,所以长度不固定),我试过fseek,fwrite好像都不行,因为我的目标和源字符串可能长度不相等,请教各位该如何做?最好能给段简单源码!谢谢了
解决方案 »
- 如何提高bmp转jpg速度
- socket connect 问题
- javascript与activex交互问题
- 接收数据之效率探讨(同步或者异步更好).
- 怎么用菜单命令切换标签页??
- 设计模式
- 求:谁有directshow编写的播放视频的源代码啊???(我要SDK编写的!!)
- clISTCTRL 的 checkbox 状态改变 的扑捉问题!
- 将数组的图像数据存成BMP,出来的图是斜的,怎么办?
- VC++6.0和ACCESS2002的小型系统如何做数据备分,恢复功能
- ◆根据这样的一个结构,来构建一个菜单的问题。想了我好多天了,郁闷,哪位大哥看一下…!
- 菜单加载的问题,解决问题后发帖再给200分,顶者有分
CString temp,ss;
file1.Open("4.txt",CFile::modeRead);
while(file1.ReadString(temp))
{
temp.Replace("192.168.88.6","192.168.88.142");
ss+=temp+"\n";
}
file1.Close();
file1.Open("4.txt",CFile::modeCreate|CFile::modeReadWrite|CFile::modeNoTruncate);
file1.WriteString(ss);
file1.Close();
应该可以,你试试吧,不过文件名是4.txt
1.记录一个未写位置nWriteIndex=0;
2.从nWriteIndex搜索192.168.88.7
3.如果没找到
3.1把从nWriteIndex到当前的位置的内容写入文件
3.2结束
4.如果找到
4.1把从nWriteIndex到当前的位置的内容写入文件
4.2把192.168.88.142写入文件
4.3修改未写入文件位置为:nWriteIndex=当前位置+strlen("192.168.88.7")
4.4重复2
需要做一个字符串比对函数定位。
CStdioFile file;
if( !file.Open("c:\\1.txt", CFile::modeRead) )
{
AfxMessageBox("can not open file!");
return;
}
CString strLine;
while(file.ReadString(strLine))
{
if(strLine.Find("192.168.88.7") != -1)
strLine.Replace("192.168.88.7", "192.168.88.142"); strarray.Add(strLine);
}
file.Close();
if( !file.Open("c:\\1.txt", CFile::modeCreate|CFile::modeWrite) )
{
AfxMessageBox("can not open file!");
return;
}
for(i=0; i<strarray.GetSize(); i++)
{
str = "";
str = strarray[i] + "\n";
file.WriteString(str);
}
file.Close();