在文件中找到某一行(字串),然后用另一字串代替原来这行,用CFile能做到吗?急!! 在文件中找到某一行(字串),然后用另一字串代替原来这行,用CFile能做到吗?如果能怎么作,还有别的方法吗?请指教!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我觉得最好的方法还是读入到CString中使用CString::Replace()函数替换后再写入文件中的方式比较直接一些 如果文件不太大是可以的,用CSting读出来,Find后替换,再写回去。 先CFile::find()查到位置和字串的大小,用SetPointer()函数设置位置,在写就好了 用CSting读出来没问题,“Find后替换,再写回去”说具体点好吗,能给个简单几行代码吗?麻烦你了!我的理解你们是说把文件中所有内容全部写到一个CString中,改了CString里的内容,在全部重写到文件中去,是这样吗?没有别的办法了吗? “用SetPointer()函数设置位置,在写就好了”可以覆盖原来的行吗? 用CString.find()找到位置然后CString.mid()取子串再把它们加到一起重复以上步骤就可以了 不行的,需要覆盖替换的:你可以把修改后新的的起个名字。修改完毕后,把原来那个删除,再把新的改称原来的名字。用CFIle函数就可以做到。 可是文件很大,不可能都读到一个String中,所以以上的办法不是很适用,有没有别的办法啊!请了! CStdioFile file; CString strLine(""); CString key; CString strNew; CString strPath; if (!file.Open(strPath, CFile::modeReadWrite)) { MessageBox("Can't parse the file"); return; } while (file.ReadString(strLine)) { if (strLine.Find(key) != -1) { int len = strLine.GetLength(); strLine = strNew; while (strLine.GetLength() != len) strLine = strLine + _T(" "); file.Seek(- len - 2, CFile::current); file.WriteString(strLine); break; } } file.Close(); CStdioFile file; CString strLine(""); CString key; CString strNew = _T("Simple line"); CString strPath; DWORD dwPosition ; if (!file.Open("D:\\test.txt", CFile::modeReadWrite)) { MessageBox("Can't parse the file"); return; } while (file.ReadString(strLine)) { dwPosition = file.GetPosition(); if (strLine.Find(key) != -1) { int len = strLine.GetLength(); strLine = strNew; dwPosition = file.GetPosition(); while (strLine.GetLength() != len) strLine = strLine + _T(" "); file.Seek(- len - 2, CFile::current); file.WriteString(strLine); file.Seek(dwPosition, CFile::begin); } } MessageBox("OK"); file.Close(); VC++和VC.net的联系和差异 socket问题,两个客户端如何通过服务器端进行通信。 VFW关于视频捕获卡的问题 关于word的编程 几个关于dll头文件的问题!!! 在对话框怎么画图 菜鸟的简单问题,请教怎么得到一个“OK”按钮的事件触发? socket程序中包含的stdafx.h是干啥的?附源程序,编译无法通过? 在Release模式下能运行的程序在DEBUG模式下有错误出现,那这样的代码有问题吗? static,抓狂中 请问如何将char转换为unsigned char? 求购ado对sql anywhere 的连接字符串
然后CString.mid()取子串
再把它们加到一起
重复以上步骤就可以了
你可以把修改后新的的起个名字。修改完毕后,把原来那个删除,再把新的改称原来的名字。
用CFIle函数就可以做到。
CString strLine("");
CString key;
CString strNew;
CString strPath; if (!file.Open(strPath, CFile::modeReadWrite))
{
MessageBox("Can't parse the file");
return;
} while (file.ReadString(strLine))
{
if (strLine.Find(key) != -1)
{
int len = strLine.GetLength();
strLine = strNew;
while (strLine.GetLength() != len)
strLine = strLine + _T(" ");
file.Seek(- len - 2, CFile::current);
file.WriteString(strLine);
break;
}
}
file.Close();
CString strLine("");
CString key;
CString strNew = _T("Simple line");
CString strPath;
DWORD dwPosition ;
if (!file.Open("D:\\test.txt", CFile::modeReadWrite))
{
MessageBox("Can't parse the file");
return;
} while (file.ReadString(strLine))
{
dwPosition = file.GetPosition();
if (strLine.Find(key) != -1)
{
int len = strLine.GetLength();
strLine = strNew;
dwPosition = file.GetPosition();
while (strLine.GetLength() != len)
strLine = strLine + _T(" ");
file.Seek(- len - 2, CFile::current);
file.WriteString(strLine);
file.Seek(dwPosition, CFile::begin);
}
}
MessageBox("OK");
file.Close();