Lazarus中的TextFile ReWrite错误

时间:2017-02-28 00:32:57

标签: lazarus

我有两个文件 - 一个包含许多短字符串(我称之为Items),另一个包含这些短字符串的ID。我还有许多其他文件,每个文件都包含一个非常长的字符串,它通过换行符分解成数千个较小的字符串。我需要找到每个大字符串文件中每个短字符串(" Item")出现的次数,以及每次点击的起始和结束位置。 我的第一个任务是删除大文件中的换行符,以便命中不跨越两个或多个较小的字符串,然后查找大字符串中每个短字符串的出现次数和开始和结束位置,并报告这些结果使用每个短字符串的ID号。 我在EInOutError (Access Denied)行显示ReWrite(ResultFile)错误(在下面代码的第二个过程的下半部分)。不知道为什么。我可以看到为第一个长字符串的结果创建的文件,但它是空白的。这是代码;实际上,非常简单明了。感谢。

procedure TForm1.OpenStrCmdBtnClick(Sender: TObject);
begin
  if OpenDialog1.execute then OpenStrEditBx.Text := OpenDialog1.FileName
else ShowMessage('Cannot open file!');
end;




procedure TForm1.FindItemfCmdBtnClick(Sender: TObject);
var
  I, LenItem, NumTimes, offset: integer;
  Extension, FileName, Dir, Str, Item, ID: string;
  TempList,  IDList, ItemList: TStringList;
  searchResult: TSearchRec;
  ResultFile: TextFile;

begin
  IDList := TStringList.Create;
  ItemList := TStringList.Create;
  IDList.LoadFromFile('D:\...\IDs.txt');
  ItemList.LoadFromFile('D:\...\Items.txt');
  try
    Dir := ExtractFilePath(OpenStrEditBx.Text);

    //concatenate each line in each str sequence to get full string
    if FindFirst(Dir + '*.txt', faAnyFile, searchResult) = 0 then
    repeat
      Extension := ExtractFileExt(OpenStrEditBx.Text);
      FileName := StringReplace(searchResult.Name, Extension, '', [rFReplaceAll, rFIgnoreCase]);//remove file extension to get only file name
      TempList := TStringList.Create;
      TempList.LoadFromFile(Dir + searchResult.Name);
      Str := '';
      for I := 1 to TempList.Count-1 do // I <> 0 to ignore ID in first line
      begin
        Str := Str + TempList[I];
        ProgressBar1.Position := (ProgressBar1.Position + 10) mod ProgressBar1.Max;
      end;
      TempList.Free;
      //Find number and location of occurrences of each Item
      for I := 0 to ItemList.Count-1 do
      begin
        Item := ItemList[I];
        LenItem := Length(Item);
        ID := IDList[I];
        NumTimes := 0;
        Offset := PosEx(Item, Str, 1);
        AssignFile(ResultFile, 'D:\...\' + FileName + '.txt');
        ReWrite(ResultFile);
        while Offset <> 0 do
        begin
          inc(NumTimes);
          if NumTimes > 0 then
             WriteLn(ResultFile, 'The ' + IntToStr(NumTimes) + 'th occurrence of ' + 'ID# ' + ID + ' is from Position# ' + IntToStr(Offset) + ' to Position# ' + IntToStr(Offset + LenItem- 1) + ' in ' + FileName);
          Offset := PosEx(Item, Str, Offset + LenItem);
        end;
        WriteLn(ResultFile, 'ID# ' + ID + ' occurs ' + IntToStr(NumTimes) + ' number of times in ' + FileName);
      end;
       CloseFile(ResultFile);
       ShowMessage(FileName + ' done!');

    until FindNext(searchResult) <> 0;
    FindClose(searchResult);

  finally
    IDList.Free;
    ItemList.Free;
  end;
end;  

0 个答案:

没有答案