现有文本文件内容
    6 2010-12-22 15:50:01 1 0 1 0
   16 2010-12-22 15:50:34 1 0 1 0
    6 2010-12-22 15:50:53 1 0 1 0
   66 2010-12-23 09:01:52 1 0 1 0
    3 2010-12-23 09:04:21 1 0 1 0
    3 2010-12-23 09:04:45 1 0 1 0
    3 2010-12-23 09:08:24 1 0 1 0
   66 2010-12-23 09:10:27 1 0 1 0
    3 2010-12-23 09:19:05 1 0 1 0
    3 2010-12-23 09:24:54 1 0 1 0
   66 2010-12-23 09:25:07 1 0 1 0
    4 2010-12-23 09:27:01 1 0 1 0
    2 2010-12-23 09:31:09 1 0 1 0
    3 2010-12-23 09:31:35 1 0 1 0
怎样读取每行记录到变量里?
如第一行,将 ‘    6’读到一个int变量里,‘2010-12-22 15:50:01’读到datatime变量里,后面的类似。

解决方案 »

  1.   

    可以定义一个动态数组的结构体,把文本内容读到tstringlist中,一行行处理,或者1笔笔读数据
    不符合要求的自己修改一下uses StrUtils;type
      TRec=record
           id:integer;
           date:TDateTime;
           f1,
           f2,
           f3,
           f4:Integer;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      i:integer;
      s:string;
      sRec:array of TRec;
      sList:TStringList;
      procedure SetF(var F:Integer);
      begin
        F:=StrToInt(RightStr(s,1));
        delete(s,Length(s)-1,2);
      end;
    begin
      sList:=TStringList.Create;
      try
      sList.LoadFromFile('c:\1.txt');
      SetLength(sRec,sList.Count);
      for i:=0 to sList.Count-1 do
      begin
        s:=Trim(sList.Strings[i]);
        sRec[i].id:=StrToInt(LeftStr(s,pos(' ',s)-1));
        delete(s,1,pos(' ',s));
        SetF(sRec[i].f4);
        SetF(sRec[i].f3);
        SetF(sRec[i].f2);
        SetF(sRec[i].f1);
        sRec[i].date:=StrToDateTime(s);
      end;
      finally
        sList.Free;
      end;  {取值测试
      for i:=low(sRec) to high(sRec) do
         memo1.Lines.Add(IntToStr(sRec[i].id)
         +' '+ DateTimeToStr(sRec[i].date)
         +' '+IntToStr(sRec[i].f1)
         +' '+IntToStr(sRec[i].f2)
         +' '+IntToStr(sRec[i].f3)
         +' '+IntToStr(sRec[i].f4)); }
    end;
      

  2.   

    方法太多,读取一行,判断第一个空格,取前面字符正则stringlist空格分割
      

  3.   

    tstringlist
    linebreak:=' ';
    text:=s
      

  4.   

    学习了,楼上用记录处理的方式很简洁。
    unit UMain;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs,StrUtils, StdCtrls;type
      TRec=record
           id:integer;
           date:TDateTime;
           f1,
           f2,
           f3,
           f4:Integer;
      end;  TFrmMain = class(TForm)
        Button1: TButton;
        Memo1: TMemo;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      FrmMain: TFrmMain;   implementation{$R *.dfm}procedure TFrmMain.Button1Click(Sender: TObject);
    var
      i:integer;
      s:string;
      sRec:array of TRec;
      sList:TStringList;
      procedure SetF(var F:Integer);
      begin
        F:=StrToInt(RightStr(s,1));
        delete(s,Length(s)-1,2);
      end;
    begin
      sList:=TStringList.Create;
      try
      sList.LoadFromFile('.\Test.txt');
      SetLength(sRec,sList.Count);
      for i:=0 to sList.Count-1 do
      begin
        s:=Trim(sList.Strings[i]);
        sRec[i].id:=StrToInt(LeftStr(s,pos(' ',s)-1));
        delete(s,1,pos(' ',s));
        SetF(sRec[i].f4);   //从后面开始删除,最后保留时间字段
        SetF(sRec[i].f3);
        SetF(sRec[i].f2);
        SetF(sRec[i].f1);
        sRec[i].date:=StrToDateTime(s);
      end;
      finally
        sList.Free;
      end;  {取值测试      }
      for i:=low(sRec) to high(sRec) do
         memo1.Lines.Add(IntToStr(sRec[i].id)
         +' '+ DateTimeToStr(sRec[i].date)
         +' '+IntToStr(sRec[i].f1)
         +' '+IntToStr(sRec[i].f2)
         +' '+IntToStr(sRec[i].f3)
         +' '+IntToStr(sRec[i].f4)); 
    end;
    end.