小白出场:各位大师傅多多关照  问题描述
已知   '.\memo.txt'memo.txt  文本文件里面有   [GOLON]   
原有字符串 [gewq]    
原有字符串 [swwwq]     
原有字符串
需要在  [GOLON]  下面写入字符串      每次保存的时候把之前写进去的字符串都删掉     但是不删除原有的字符串
[GOLON] 
字符串1
字符串2
字符串3
字符串4原有字符串大神赐教  最好给个例子  研究研究

解决方案 »

  1.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Memo1: TMemo;
        Button1: TButton;
        Edit1: TEdit;
        Memo2: TMemo;
        ComboBox1: TComboBox;
        Button3: TButton;
        Button4: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
        procedure Button4Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure SetText(FileName:string;title,S:string);    // 函数
    var MyList:TStringList;
        i,j,k:integer;label X;begin
      MyList:=TStringList.Create;
      MyList.LoadFromFile(FileName);
      For i:=0 to Mylist.Count-1 do
      begin
        if Trim(MyList.Strings[i])=Trim(title) then
        begin
          // 找到尾部
          j:=i;
          repeat
            j:=J+1;
          until (j=Mylist.Count-1) or (Copy(Trim(MyList.Strings[j]),1,1)='[');
          Showmessage(inttostr(j));
          if J>(I+1) then     // 删除
          begin
            for k:=j-1 downto i+1 do
              Mylist.Delete(k);
          end;
          if i=Mylist.Count-1  // 加入新字符
            then Mylist.Add(S)
            else Mylist.Insert(i+1,S);
          goto X;
        end;
      end;
    X:
      MyList.SaveToFile(FileName);
      Mylist.Free;
    end;procedure TForm1.Button1Click(Sender: TObject);         //处理
    var FileName,Title,S:string;
    begin
      FileName:='D:\memo.txt';
      Title:=Combobox1.text;
      S:=Memo2.text;
      SetText(FileName,title,S);
      Memo1.Lines.LoadFromFile(Filename);
    end;procedure TForm1.Button3Click(Sender: TObject);           // 读入
    var FileName,Title,S:string;
    begin
      FileName:='D:\memo.txt';
      Memo1.Lines.LoadFromFile(Filename);
    end;procedure TForm1.Button4Click(Sender: TObject);           // 写入
    var FileName,Title,S:string;
    begin
      FileName:='D:\memo.txt';
      Memo1.Lines.SaveToFile(Filename);
    end;end.