如何用TSringlist取得txt文件中的行数?

解决方案 »

  1.   

    txt文件格式如下:学生1
    学生2
    学生3
    学生4
      

  2.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      tips:tstringlist;
    begin
      tips:=tstringlist.create;
      tips.loadfromfile('filename.txt');
      showmessage(inttostr(tips.Count));
    end;
      

  3.   

    var FileList:TStringList;
    begin
      FileList:=TStringList.Creat;
      Try
        FileList.Clear;
        FileList.LoadFromFile('文件名');
        ShowMessage('这个文件的行数是'+IntToStr(FileList.Count))
      Finally
        FileList.Free
      End;
    这样写规范一些
      

  4.   

    procedure TForm1.FileOper(fileName: string);
    var
      FileObj:Tstringlist;
    begin
      fileobj:=TStringList.Create ;
      try
      fileobj.LoadFromFile(fileName);
      fileobj.Add('这里将增加一个新行');
      showmessage(inttostr(fileobj.Count));       '显示行数
      //如要删除某一行,如第二行
      fileobj.Delete(1);
      showmessage(inttostr(fileobj.Count));       '显示行数
      fileobj.SaveToFile(fileName);
      finally
        freeandnil(fileobj);
      end;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
       fileoper('c:\aa.txt') ;
    end;
    测试通过