Domanda

I'm making a filter program (a pretty simple one) in free pascal and I'm reading the source text from a huge textfile. If I use string, everything works fine, but it's limited to 255. If I try to use ansiString, it gives me error 201.

Code:

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.
È stato utile?

Soluzione

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:

  • An array was accessed with an index outside its declared range.
  • Trying to assign a value to a variable outside its range (for instance an enumerated type).

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top