I know there are more elegant variations but these should be simple and understandable.
try your variables is not not to be confused
Assignfile ( filehan, 'EMP.txt');
Reset ( filehand );
CloseFile (FFile);
One filehan
and the other filehand
and FFile
The particular characteristics of a TStringList, your SubStrings function needs only one line of code.
sList.text:=StringReplace(AString,ADelimiter,#13#10,[rfReplaceAll]);
- only 4 variables.
- only 1 TStringList .
Tested with Delphi 5
var
LineOfText : String;
sList : TStringList;
filehand,Outfilehand: text;
function SubStrings(AString: String; ADelimiter: Char; var sList: TStringList):Boolean;
begin
sList.text:=StringReplace(AString,ADelimiter,#13#10,[rfReplaceAll]);
end;
begin
sList := TStringList.Create;
Assignfile ( filehand, 'EMP.txt');
Assignfile ( Outfilehand, 'FSTRING.txt');
Reset ( filehand );
Rewrite(Outfilehand);
try
While not EOF(filehand) do
begin
ReadLn ( filehand, LineOfText );
LineOfText:=StringReplace(LineOfText,': ',':',[rfReplaceAll]);
LineOfText:=StringReplace(LineOfText,' :',':',[rfReplaceAll]);
if pos(':210:',LineOfText)>0 then begin
substrings(LineOfText, ':' ,sList);
if (sList.Count=6) then
if (sList[3]='210') then writeln(Outfilehand,sList[0]+';'+sList[1]);
end;
end;
finally
sList.Free;
CloseFile (filehand);
CloseFile (Outfilehand);
end;
UPDATE:
I understood the question so that you are only in the row with 210
field value are interested. If you want all the lines in the new file, replace .
if pos(':210:',LineOfText)>0 then begin
substrings(LineOfText, ':' ,sList);
if (sList.Count=6) then
if (sList[3]='210') then writeln(Outfilehand,sList[0]+';'+sList[1]);
end;
with
substrings(LineOfText, ':' ,sList);
if (sList.Count=6) then
if (sList[3]='210') then
writeln(Outfilehand,sList[0]+';'+sList[1]+';'+sList[4]+';'+sList[5]);
if (sList.Count=3) then
writeln(Outfilehand,sList[0]+';'+sList[1]);
Based on the size or value of sList[3] :
- You can self define which fields are written to
FSTRING.txt
.
UPDATE2:
How I would do it.
var
ParsedText : String;
inList,outList,sList : TStringList;
i:integer;
function SubStrings(lineText: String; var sList: TStringList):String;
begin
result:='ERROR';
lineText:=StringReplace(lineText,': ',':',[rfReplaceAll]);
lineText:=StringReplace(lineText,' :',':',[rfReplaceAll]);
sList.text:=StringReplace(lineText,':',#13#10,[rfReplaceAll]);
if (sList.Count=6) then
if (sList[3]='210') then result:=sList[0]+';'+sList[1]+';'+sList[4]+';'+sList[5] else
result:=sList[0]+';'+sList[1];
if (sList.Count=3) then result:=sList[0]+';'+sList[1];
end;
begin
inList := TStringList.Create;
outList := TStringList.Create;
sList := TStringList.Create;
try
inList.LoadFromFile('EMP.txt');
for i:= 0 to inList.Count-1 do begin
ParsedText := SubStrings(inList[i],sList);
if ParsedText <> 'ERROR' then outList.Add(ParsedText);
end;
outList.SaveToFile('FSTRING.txt');
finally
inList.Free;
outList.Free;
sList.Free;
end;
end;