procedure TForm1.Button1Click(Sender: TObject);
var
  TextFileVar: Textfile ;
  fileN: string;
  id, hz,bm,temp: string;
begin
  FileN:='hanzi.txt';
  AssignFile ( TextFileVar , FileN ) ;
  Reset(TextFileVar);
  table1.open;
  table1.edit;
  while not SeekEof(TextFileVar) do
  begin
    Readln ( TextFileVar , temp ) ;
    id:=copy(temp, 1, pos(' ',temp)-1);
    temp:=copy(temp,pos(' ',temp)+1,length(temp)-pos(' ',temp));
    hz:=copy(temp, 1, pos(' ',temp)-1);
    bm:=copy(temp,pos(' ',temp)+1,length(temp)-pos(' ',temp));
   table1.append;
    table1.fields[0].AsInteger:=StrToInt(id);
    table1.fields[1].AsString:=hz;
    table1.fields[2].AsString:=bm;
  end;
  table1.post;
  closeFile(TextFileVar);
end;转自大富翁论坛

解决方案 »

  1.   

    打开一个已有的文本文件,并将内容放到stringgrid中:
    文本行与stringgrid行一致;
    在文本中遇到空格则放入下一cells.
    rocedure TForm1.Button1Click(Sender: TObject);
    var
      aa,bb:tstringlist;
      i:integer;
    begin
    aa:=tstringlist.Create;
    bb:=tstringlist.Create;
    aa.LoadFromFile('c:\windows\desktop\aa.txt');
    for i:=0 to aa.Count-1 do
    begin
    bb:=SplitString(aa.Strings[i],' ');
    stringgrid1.Rows[i]:=bb;
    end;
    aa.Free;
    bb.Free;
    end;其中splitstring为:
    function SplitString(const source,ch:string):tstringlist;
    var
    temp:string;
    i:integer;
    begin
    result:=tstringlist.Create;
    temp:=source;
    i:=pos(ch,source);
    while i<>0 do
    begin
      result.Add(copy(temp,0,i-1));
      delete(temp,1,i);
      i:=pos(ch,temp);
    end;
    result.Add(temp);
    end;