怎么实现删除txt文件的某一行信息,修改某一行信息
#include "stdafx.h"
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
try
{
CStdioFile
file;
LPCTSTR
pszFile = _T("F:\\11.txt");
file.Open(pszFile,
CFile::modeRead);
CString
strText;
DWORD
dwPos = 0;
LPCTSTR
pszSrc = _T("111111");
LPCTSTR
pszDes = _T("222222\n");
while(file.ReadString(strText))
{
if(strText
== pszSrc)
{
dwPos = (DWORD)file.GetPosition() - (_tcslen(pszSrc) +
_tcslen(_T("\r\n")));
break;
}
}
file.Close();
file.Open(pszFile,
CFile::modeWrite);
file.Seek(dwPos,
CFile::begin);
file.WriteString(pszDes);
file.Close();
}
catch(CException* e)
{
e->ReportError();
e->Delete();
}
return
0;
}
// 11.txt (ANSI文本文件)
aaaaaa
111111
333333
444444
程序运行之后文件内容
aaaaaa
222222
333333
444444
加载中,请稍候......