• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
汇小流以成江海,积毫沙乃成高塔
博客园    首页    新随笔    联系   管理    订阅  订阅

VC++ 删除当前读取行 代码

操作Text,要删除当前读取的行。(VC 文件操作DEMO)

我想到两种方法:         

1.生成临时文件 读一行 写进去 跳过要删除的行

2.全部读入内存 删除 然后写回txt文件

 第一种方法上代码:

View Code
 1 void CPoint7Dlg::OnBnClickedButton5()
 2 {
 3    
 4     CString     strPathName;   
 5     GetModuleFileName(NULL,strPathName.GetBuffer(256),256);   
 6     strPathName.ReleaseBuffer(256);   
 7     int nPos = strPathName.ReverseFind('\\');   
 8     strPathName = strPathName.Left(nPos +1);
 9 
10     CStdioFile file;
11     CString str;
12     CString firstStr;
13     CString strFull;
14     str.Format(strPathName+"Log\\1.txt",g_BasePath);
15     if (file.Open(_T(str),CFile::modeRead | CFile::typeText))
16     {
17         file.SeekToBegin();
18         int i=1;
19         while(file.ReadString(str))
20         {    
21             if(i>1)
22             {
23               strFull+=str+"\n";
24             }
25             i++;
26         }        
27         file.Close();
28         MessageBox(strFull);
29     }
30     else
31     {
32         MessageBox(_T("data1.txt文件打开失败"));
33     }
34 
35     strPathName;   
36     GetModuleFileName(NULL,strPathName.GetBuffer(256),256);   
37     strPathName.ReleaseBuffer(256);   
38     nPos = strPathName.ReverseFind('\\');   
39     strPathName = strPathName.Left(nPos +1);
40 
41 
42     str.Format(strPathName+"Log\\1.txt",g_BasePath);
43    
44     if (!file.Open(_T(str),CFile::modeCreate | CFile::modeWrite | CFile::typeText))
45     {
46         MessageBox(_T("未打开文件"));
47     }
48     else
49     {      
50        file.WriteString(strFull);
51        file.Close();
52     }
53 }
posted @ 2012-11-23 10:39  小流  阅读(275)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3