I would like to scan a log file in read only mode w no locking. I have tried with FileStream, AssignFile and even CopyFile. My issue is that there is a process running that needs to write to the file which may be during the time I am reading. I have the file open in fmOpenRead or fmShareDenynone and have no issues with opening. However, the app that needs to write to the file errors out w 'Cannot open log file C:\XXX.log' -- it's trying to open the same file in fmOpenReadWrite or fmShareCompat.. any ideas? (I can't change the code for the app that's writing to the log)
Sample code:
I'm coding in Delphi and using the Windows and SysUtils units:
try
fFileStreamOne := TFileStream.Create( fFileName, fmOpenRead or fmShareDenyNone );
fFileStreamTwo := TFileStream.Create( fFileName, fmOpenReadWrite or fmShareCompat );
finally
fFileStreamOne.Destroy;
// fFileStreamTwo.Destroy; -- exception
end;
(if both would be open for fmRead or fmShareDenyNone there are no issues)
Other failed solutions:
try
FileHandle := FileOpen(fFileName, fmOpenRead or fmShareDenyNone);
fFileStreamTwo := TFileStream.Create( fFileName, fmOpenReadWrite or fmShareCompat );
finally
FileClose (FileHandle);
// fFileStreamTwo.Destroy;
end;
-----------------------
try
AssignFile(F, fFileName); { File selected in dialog box }
FileMode := 0; {Set file access to read only }
with TTextRec(f) do begin
// else we try generic_read, file_share_read or file_share_write
handle:=FileOpen(fFileName,fmShareDenyNone);
if dword(handle)=invalid_handle_value then begin
raise EInOutError.CreateResFmt(@SInOutError, [GetLastError]);
end;
mode:=fmInput;
BufPos:=0;
BufEnd:=0;
end;
fFileStreamTwo := TFileStream.Create( fFileName, fmOpenReadWrite or fmShareCompat );
finally
CloseFile(F);
// fFileStreamTwo.Destroy; // exception
end;
-------------------------------------------
try
AssignFile(F, fFileName); { File selected in dialog box }
FileMode := 0; {Set file access to read only }
Reset(F);
Readln(F, S); { Read the first line out of the file }
Readln(F, S); { Read the second line out of the file }
// try open for stream writing:
fFileStreamTwo := TFileStream.Create( fFileName, fmOpenReadWrite or fmShareCompat );
finally
CloseFile(F);
// fFileStreamTwo.Destroy; // (exception)
end;
Sample code:
I'm coding in Delphi and using the Windows and SysUtils units:
try
fFileStreamOne := TFileStream.Create( fFileName, fmOpenRead or fmShareDenyNone );
fFileStreamTwo := TFileStream.Create( fFileName, fmOpenReadWrite or fmShareCompat );
finally
fFileStreamOne.Destroy;
// fFileStreamTwo.Destroy; -- exception
end;
(if both would be open for fmRead or fmShareDenyNone there are no issues)
Other failed solutions:
try
FileHandle := FileOpen(fFileName, fmOpenRead or fmShareDenyNone);
fFileStreamTwo := TFileStream.Create( fFileName, fmOpenReadWrite or fmShareCompat );
finally
FileClose (FileHandle);
// fFileStreamTwo.Destroy;
end;
-----------------------
try
AssignFile(F, fFileName); { File selected in dialog box }
FileMode := 0; {Set file access to read only }
with TTextRec(f) do begin
// else we try generic_read, file_share_read or file_share_write
handle:=FileOpen(fFileName,fmShareDenyNone);
if dword(handle)=invalid_handle_value then begin
raise EInOutError.CreateResFmt(@SInOutError, [GetLastError]);
end;
mode:=fmInput;
BufPos:=0;
BufEnd:=0;
end;
fFileStreamTwo := TFileStream.Create( fFileName, fmOpenReadWrite or fmShareCompat );
finally
CloseFile(F);
// fFileStreamTwo.Destroy; // exception
end;
-------------------------------------------
try
AssignFile(F, fFileName); { File selected in dialog box }
FileMode := 0; {Set file access to read only }
Reset(F);
Readln(F, S); { Read the first line out of the file }
Readln(F, S); { Read the second line out of the file }
// try open for stream writing:
fFileStreamTwo := TFileStream.Create( fFileName, fmOpenReadWrite or fmShareCompat );
finally
CloseFile(F);
// fFileStreamTwo.Destroy; // (exception)
end;