注册 登录
编程论坛 VC++/MFC

怎么实现删除txt文件的某一行信息,修改某一行信息

shendade 发布于 2012-06-10 14:40, 1811 次点击
#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
这是网友发的,,但我看不打懂,,谁能解释下,,或者给个新方法
0 回复
1