I’m making a filter program (a pretty simple one) in Free Pascal and I’m reading the source text from a huge text file. If I use string, everything works fine, but it’s limited to a length of 255 characters. If I try to use ANSIString, it gives me the error 201.
program TextFilter;
uses Crt;
Var
_input, filter,
tilStar, afterStar,
output : string;
input : ansistring;
i, j : longint;
have : boolean;
fileIn, fileOut : text;
Function cutDownText(orig:ansistring; til:integer) : string;
var
new : ansistring;
i : integer;
begin
new:='';
for i:=1 to length(orig) do
if i>til then
new:=new+orig[i];
cutDownText:=new;
end;
Begin
ClrScr;
writeln('K‚rem a szűr‹t:');
readln(filter);
Assign(fileIn, 'C:\DEVELOPMENT\Pascal\source.txt');
Assign(fileOut, 'C:\DEVELOPMENT\Pascal\output.txt');
reset(fileIn);
rewrite(fileOut);
tilStar:='';
afterStar:='';
output:='';
have:=true;
input:='';
while not(eof(fileIn)) do begin
readln(fileIn, _input);
input:=input+_input;
end;
close(fileIn);
for i:=1 to length(filter) do
if filter[i]='*' then
break
else
tilStar:=tilStar+filter[i];
for i:=i to length(filter) do
if not(filter[i]='*') then
afterStar:=afterStar+filter[i];
while(have=true) do begin
if (Pos(tilStar, input)>0) then begin
if (Pos(tilStar, input) > Pos(afterStar, input)) then begin
for j:=(Pos(tilStar, input)+length(tilStar)) to length(input) do begin
output:=output+input[j];
end;
writeln(fileOut, output);
output:='';
input:=cutDownText(input, (j + length(afterStar)));
end else begin
for j:=(Pos(tilStar, input)+length(tilStar)) to (Pos(afterStar, input)-1) do begin
output:=output+input[j];
end;
writeln(fileOut,output);
output:='';
input:=cutDownText(input, (j + length(afterStar)));
end;
end else
have:=false;
end;
close(fileOut);
readkey;
End.
Error 201 means "Range check error". If you compiled your program with range checking on, then you can get this error in the following cases:
Output and cutDownText are declared as string. If you don't compile the program with the {$H+} option, string is limited to 255 characters, but you could trying to copy more than 255 chars from the input ansistring.
Try to compile with {$H+} or convert string to ansistring everywhere.