如果文本不是太大的话,用memo.lines.loadfromfile读入,然后用memo.lines[i]去一行一行的 处理。至于如何读取字段记录,可以定义一个数组, var FieldValue : Array of String;setlength(FieldValue,4); for J := 0 to memo1.lines.count do begin tmpstr := memo1.lines[J]; for I := 0 to 3 do begin FieldValue[I] := copy(tmpstr,1,pos(',',tmpstr) - 1); tmpstr := copy(tmpstr,pos(',',tmpstr),length(tmpstr) - pos(',',tmpstr)); end; end;
var f:TxtFile; s:string; List:TStringList;//保存你所需的数据 begin List:=TStringList.create; AssignFile(f,'c:\test.txt'); if fileexists(f) then begin reset(f); while eof(f) do begin readln(f,s); if pos(',',s)>0 then begin List.add(Copy(s,1,pos(',',s))); delete(s,1,pos(',',s)); end else List.add(s); end; end; List.free; end;
逗号
换行符号啊用readln(....)就可以了;用reset打开:)
先获得每个字段的长度;就知道了字符串;去读就可以了
var
FieldValue : Array of String;setlength(FieldValue,4);
for J := 0 to memo1.lines.count do
begin
tmpstr := memo1.lines[J]; for I := 0 to 3 do
begin
FieldValue[I] := copy(tmpstr,1,pos(',',tmpstr) - 1);
tmpstr := copy(tmpstr,pos(',',tmpstr),length(tmpstr) - pos(',',tmpstr));
end;
end;
s:string;
List:TStringList;//保存你所需的数据
begin
List:=TStringList.create;
AssignFile(f,'c:\test.txt');
if fileexists(f) then
begin
reset(f);
while eof(f) do
begin
readln(f,s);
if pos(',',s)>0 then
begin
List.add(Copy(s,1,pos(',',s)));
delete(s,1,pos(',',s));
end
else
List.add(s);
end;
end;
List.free;
end;
使用Tstringlist最简单!
TMyFileRecord = record
Field1: string[3];
Comma1: char;
Field2: Integer;
Comma2: char;
Field3: Integer;
Comma3: char;
Field4: string[8];
Comma4: char;
Field5: string[4];
end;
TMyFile = file of TMyFilerecord;var
MyFileRecord: TMyFileRecord;
Myfile: TMyFile;
begin
AssignFile(MyFile, {FileName});
Reset(MyFile);
ReadLn(MyFile, MyFileRecord);
...
CloseFile(MyFile);
end;
最好事先知道有几个字段(逗号),然后用循环读取特定两个逗号之间的字符。