Recent

Author Topic: [Solved] Exception while loading a file  (Read 4982 times)

OLLI_S

  • Full Member
  • ***
  • Posts: 119
[Solved] Exception while loading a file
« on: November 14, 2018, 02:24:57 pm »
Hello,

I am using Lazarus 1.8.4 in Windows 10 Professional x64.

In my form I have a button, that loads a log-file into a TStringlist.
The log file is located in the Windows Log-Folder (C:\WINDOWS\Logs\filename.log) and is created by an other application.
I just read it and show some lines in a TMemo control.

Code: Pascal  [Select][+][-]
  1. procedure TfrmMainForm.btnRealodLogFileClick(Sender: TObject);
  2.  
  3. var
  4.   LogContent : TStringlist;
  5.  
  6. begin
  7.  
  8.   // Create the String-List that contains the complete Log-File
  9.   LogContent := TStringlist.create;
  10.  
  11.   LogFileName := 'C:\WINDOWS\Logs\filename.log';
  12.  
  13.   // Check if the file exists
  14.   if not (FileExists(LogFileName)) then begin
  15.  
  16.     MessageDlg('File not found', 'The following file can not be found:' + #13 + LogFileName, mtError, [mbOK], 0);
  17.  
  18.     // Free the String-List and exit the procedure
  19.     LogContent.Free;
  20.     Exit;
  21.  
  22.   end;
  23.  
  24.   try
  25.  
  26.     // Try to load the Log-File in the String-List
  27.     LogContent.LoadFromFile(LogFileName);
  28.  
  29.   except
  30.  
  31.     on E: EFOpenError do begin
  32.  
  33.       // Error opening the file
  34.       MessageDlg('File not accessible', 'The Log-File is not accessible!', mtError, [mbOK], 0);
  35.  
  36.     // Free the String-List and exit the procedure
  37.     LogContent.Free;
  38.     Exit;
  39.  
  40.     end; // of EFOpenError
  41.  
  42.   end; // of except
  43.  
  44.   // Show the Log-File in the Memo-Control
  45.   mmoLogFile.Lines.Clear;
  46.   mmoLogFile.Lines := LogContent;
  47.  
  48.   // Free the String List
  49.   LogContent.free;
  50.  
  51. end;

This works without any problems.
But sometimes, when the other application (that is creating the log file) is writing in the file, then I get the EFOpenError exception.

But why?
I just access it in reading mode!
Or am I wrong?

Best regards

OLLI
« Last Edit: November 17, 2018, 04:08:00 pm by OLLI_S »

fred

  • Full Member
  • ***
  • Posts: 201
Re: Exception while loading a file
« Reply #1 on: November 14, 2018, 02:37:29 pm »
LoadFromFile uses:
Code: Pascal  [Select][+][-]
  1.   TheStream:=TFileStream.Create(FileName,fmOpenRead or fmShareDenyWrite);
So while reading the other can't open for writing (fmShareDenyWrite)

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Exception while loading a file
« Reply #2 on: November 14, 2018, 02:41:28 pm »
LoadFromFile tries to open the file stream in mode: fmOpenRead or fmShareDenyWrite so as to be able to read the whole file at once. If another process is writing to the file this fails with the exception you see.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Exception while loading a file
« Reply #3 on: November 14, 2018, 03:31:23 pm »
LoadFromFile tries to open the file stream in mode: fmOpenRead or fmShareDenyWrite so as to be able to read the whole file at once. If another process is writing to the file this fails with the exception you see.
Indeed and it is very likely the file is locked by the other logging application(s). So use a TFileStream with fmOpenRead or ShareDenyNone may help. You can subsequently use TStringlist.LoadFromStream on that stream.
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. uses sysutils,classes;
  3. var
  4.   s:TFileStream;
  5.   l:TStringlist;
  6. begin
  7.   s:=  TFilestream.Create('/var/log/syslog', fmOpenRead or fmShareDenyNone);
  8.   try
  9.     l := TStringlist.Create;
  10.     try
  11.       l.LoadFromStream(s);
  12.       writeln(l.text);
  13.     finally
  14.       l.free;
  15.     end;
  16.   finally
  17.     s.free;
  18.   end;
  19. end.
« Last Edit: November 14, 2018, 05:00:06 pm by Thaddy »
Specialize a type, not a var.

OLLI_S

  • Full Member
  • ***
  • Posts: 119
Re: Exception while loading a file
« Reply #4 on: November 17, 2018, 04:07:33 pm »
LoadFromFile tries to open the file stream in mode: fmOpenRead or fmShareDenyWrite so as to be able to read the whole file at once. If another process is writing to the file this fails with the exception you see.

OK, thank you!
I just catch an exception and write a message to the user....
Issue solved.

 

TinyPortal © 2005-2018