资讯   |   开发   |   选机中心   |   产品大全 | IBM | 惠普 | 联想 | 戴尔 | 苹果 | 神舟
更多: | 华硕 | 明基 | 方正 | 紫光 | TCL | 夏新 | 联宝 | 宏碁 | 七喜 | 长城 | 清华同方 | 海尔 | 三星 | 东芝 | 索尼 | 富士通 | LG | 技术 | ddnoon
当前位置:笔记本 > 软件开发 >
Advertisement
文章正文

怎么才能修改文件中的一部分内容?谢谢了!

类型:转载   责任编辑:asp.net   日期:2007/05/23


热门软件下载:


   

我先用fstream   open打开了一个文件,然后用seek定位到相应位置,问题是现在不知道怎么替换,如果用cout<<的话会破坏文件中其他不需要替换的内容?谢谢了

网友回答:

发表者:lifang276

看帮助里的  
      TReplaceFlags   replaceflags;  
      replaceflags     <<   rfReplaceAll;  
  StringReplace  
   
  可以替代指定的内容  
   
  如  
  StringReplace(TmpStr1,"[总记录数]",IntToStr(jlcount),replaceflags);  
  把tmpstr1中的"[总记录数]"用IntToStr(jlcount)替换

发表者:COpyFRee

//写了一个很简单的例子,只修改了文件中的一个字节,你可以自己改一下。  
  void   __fastcall   TForm1::Button1Click(TObject   *Sender)  
  {  
          int   iFileHandle;  
          int   Length   =   1;  
          char   *Buffer;  
          AnsiString   DataFile   =   "D:\\a.txt";  
   
          if   (FileExists(DataFile))  
          {  
                  iFileHandle   =   FileOpen(DataFile,   fmOpenReadWrite);  
          }  
   
          if   (iFileHandle)  
          {  
                  Buffer   =   new   char[1];  
   
                  FileSeek(iFileHandle,   5,   0);  
                  FileRead(iFileHandle,   Buffer,   Length);  
   
                  *Buffer   =   a;  
   
                  FileWrite(iFileHandle,   Buffer,   Length);  
                  FileClose(iFileHandle);  
   
                  delete   Buffer;  
          }  
  }

发表者:pepsi1980

以下是删除文件中某行  
   
  CStringArray   strarray;  
  CStdioFile   file;  
  if(   !file.Open("c:\\1.txt",   CFile::modeRead)   )  
  {  
  AfxMessageBox("can   not   open   file!");  
  return;  
  }  
  CString   strLine;  
  while(file.ReadString(strLine))  
  {  
  strarray.Add(strLine);  
  }  
  file.Close();  
   
  CString   str;  
  int   nCount=strarray.GetSize();  
  for(int   i=0;   i<nCount;   i++)  
  {  
  str   =   strarray[i];  
  if(   str.Find("key")   !=   -1)  
  {  
  strarray.RemoveAt(i);//删除  
  nCount--;  
  i--;  
                                                        //这里修改  
                                                            int   nIndex   =   str.Find(f);  
                                                        if(nIndex!=-1)  
                                                        {  
                                                            CString   str1   =   str.Right(str.GetLength()-nIndex-1);  
                                                            }  
  }  
  }  
  if(   !file.Open("c:\\1.txt",   CFile::modeCreate|CFile::modeWrite)   )  
  {  
  AfxMessageBox("can   not   open   file!");  
  return;  
  }  
  for(i=0;   i<strarray.GetSize();   i++)  
  {  
  str   =   "";  
  str   =   strarray[i]   +   "\n";  
  file.WriteString(str);  
  }  
  file.Close();


 

 
热门推荐笔记本: IBM笔记本
相关文章:
笔记本相关:
IT技术文章:
webmaster:popbb@126.com   最佳浏览:1024X768 MSIE
©2007 popbb.net All Rights Reserved