再请教一个问题,在delphi中如何读取内容像如下结构的文本文件?也就是说每行的日期列的值读到一个字符串变量,第二列读到一个字符变量,第三列读到另一个字符变量。
2002/03/01 Y B
2002/03/02 X B
...

解决方案 »

  1.   

    TStrings.LoadFromFile就可以了,每个读到一个串中
      

  2.   

    tmp_str:='2002/03/01';
    str_1:=copy(tmp_str,1,pos('/',tmp_str);//pos找到第一个'/'的位置的函数
    tmp_str:=trim(copy(pos('/',tmp_str)+1,200));
    str_2:=copy(tmp_str,1,pos('/',tmp_str);
    tmp_str:=trim(copy(pos('/',tmp_str)+1,200));
    str_2:=trim(copy(tmp_str,1,pos('/',tmp_str));
      

  3.   

    Read(F , V1 [, V2,...,Vn ] );
      

  4.   


    下面这个函数,以空格为分界符,将一行的文本写入到一个数组里 
    再根据列值取出,你可以做适当的数据类型转换,固定的文件格式你可以根据楼上的建议读入到一个TStrlist类型里。就如你的例子而言,将你的文件格式读一个TSTringList变量MyList中之后,2002/03/01 Y B
    2002/03/02 X B如果你想将第二行,第二列的X读入到变量mystr中,则可以这样调用函数:
    Mystr:=SlitStr(Mylist[1],2);注意函数以空格为分界符,你可以根据你的文件格式自已修改。 
    function  splitstr(str:string;column:integer):string; 
          var 
              Nowstr:string; 
              tempstr:string; 
              temparray:array  of  string; 
              iCount:integer; 
              NowPos:integer; 
    begin 
              iCount:=0; 
              NowStr:=trim(str); 
              NowPos:=Pos('  ',Nowstr); 
              while  Nowpos<>0  do 
                  begin 
                     inc(iCount); 
                     setlength(temparray,iCount); 
                     tempArray[iCount-1]:=copy(Nowstr,1,NowPos-1); 
                     NowStr:=copy(NowStr,nowpos+1,length(Nowstr)-nowpos); 
                     NowPos:=pos('  ',Nowstr); 
                 end; 
              inc(iCount); 
              setlength(temparray,icount); 
              temparray[Icount-1]:=NowStr; 
              result:=temparray[column-1]; 
     end;