积极答复者
txt 文件内容在修改

问题
答案
-
如果文件不大的话,您可以使用CFile类一次性将文件读入到缓冲区中,然后利用CString::Replace()替换,或者其他函数,最后再写入文件中。如果文件过大的话,可以考虑使用内存映射的方法去做。
try
{
CFile file;
file.Open(_T("F:\\11.txt"), CFile::modeReadWrite);
DWORD len = static_cast<DWORD>(file.GetLength());
char* buf = NULL;
buf = new char[len+1];
memset(buf, 0, sizeof(char) * (len+1));
file.Read(buf, len);CString strText(buf);
strText.Replace(_T("资源"), _T("材料"));delete[] buf;
buf = NULL;USES_CONVERSION;
buf = T2A(strText.GetBuffer(strText.GetLength()));
strText.ReleaseBuffer(-1);
file.SetLength(0);
file.Write(buf, strlen(buf));file.Close();
}
catch (CException* e)
{
e->ReportError();
e->Delete();
}
Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.- 已标记为答案 Warden_ 2011年3月7日 13:23
全部回复
-
如果文件不大的话,您可以使用CFile类一次性将文件读入到缓冲区中,然后利用CString::Replace()替换,或者其他函数,最后再写入文件中。如果文件过大的话,可以考虑使用内存映射的方法去做。
try
{
CFile file;
file.Open(_T("F:\\11.txt"), CFile::modeReadWrite);
DWORD len = static_cast<DWORD>(file.GetLength());
char* buf = NULL;
buf = new char[len+1];
memset(buf, 0, sizeof(char) * (len+1));
file.Read(buf, len);CString strText(buf);
strText.Replace(_T("资源"), _T("材料"));delete[] buf;
buf = NULL;USES_CONVERSION;
buf = T2A(strText.GetBuffer(strText.GetLength()));
strText.ReleaseBuffer(-1);
file.SetLength(0);
file.Write(buf, strlen(buf));file.Close();
}
catch (CException* e)
{
e->ReportError();
e->Delete();
}
Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.- 已标记为答案 Warden_ 2011年3月7日 13:23