解析文本文件的Pascal分段错误

时间:2016-03-11 09:57:18

标签: pascal lazarus freepascal

我正在使用Pascal / Lazarus中的问题/答案UI应用程序。我的问题是,通过单击按钮调用下面的代码,程序崩溃时出现Segmentation Fault错误。

// more declarations... (UI Form, Buttons, ...)

type
  TQuestion = class(TObject)
    title: string;
    answers: array of string;
    correct: integer;
  end;

var
  questions: array of TQuestion;

procedure TForm1.BStartClick(Sender: TObject);
var
  i: integer;
  j: integer;
  line: string;
  arrayLength: integer;
  question: TQuestion;
  stringList: TStringList;
begin
  stringList := TStringList.create;
  stringList.LoadFromFile('questions.txt');

  for i := 0 to stringList.Count - 1 do ;
  begin
    line := stringList[i];
    if (length(line) >= 2) then
      if (line[2] = ' ') and ((line[1] = '-') or (line[1] = '+')) then
      begin
        arrayLength := length(question.answers);
        SetLength(question.answers, arrayLength + 1);
        question.answers[arrayLength] :=
          Copy(line, 2, Length(line) - 1);

        if zeile[1] = '+' then
          question.correct := arrayLength;
      end
      else
      begin
        question := TQuestion.Create;
        question.title := line;

        arrayLength := length(questions);
        setLength(questions, arrayLength + 1);
        questions[arrayLength] := question;
      end;
  end;
  BStart.Visible := False;
end;

1 个答案:

答案 0 :(得分:1)

嗯,我的Pascal知识可以追溯到10到15年前。但是,我可以看到你在这一行的末尾有一个额外的分号:

for i := 0 to stringList.Count - 1 do ;