Wilfried Mestdagh wrote:
> Hello,
> 
> I have a compiler warning in following code 'F might not be
> initialized', at the M.LoadFromStream(F); line.
> 
>   M := TMemoryStream.Create;
>   try
>     for n := 0 to FFiles.Count - 1 do begin
>       try
>         F := TFileStream.Create(FFiles[n], fmOpenRead or fmShareDenyWrite);
>       except
>         TriggerWarning('Could not open ''' + FFiles[n] + '''');
>         Continue;
>       end;
>       M.LoadFromStream(F);
>       F.Free;

All the above can be replaced with this:

try
   M.LoadFromFile(FFiles[n]);
except
   on EFileStreamError do begin
     TriggerWarning(...);
     continue;
   end;
end;

(The exact exception class might not be accurate for all versions of 
Delphi. I don't remember EFileStreamError from Delphi 5, but it's in 
Delphi 2005.)

>       if not DeleteFile(FFiles[n]) then
>         TriggerWarning('Cannot delete file ''' + FFiles[n] + ''', will not 
> transmit it.')
>       else
>         // Todo: add to database and transmit it
>     end;
>   finally
>     M.Free;
>   end;
> 
> However unless my brain is damaged due to late hours work, it should
> always be initialized because if exception occure I 'continue' the loop.

The compiler sometimes has trouble following execution paths into or out 
of "except" blocks for the purpose of detecting usage of variables.

> What I'm trying to do is read files generated by another application,
> open them, do something with it and delete it. The other application may
> not change the file after I open it of course.

You should take a look at the File_Flag_Delete_On_Close flag, which you 
can pass to CreateFile when you open a file. You can keep the file open 
as long as you want, and when you close it, the OS will delete it for 
you. It makes sure no one else opens it without also specifying "delete" 
permission. Instead of using a TFileStream or a TMemoryStream, you would 
need to call CreateFile directly, and then perhaps use the returned 
handle in a THandleStream.

-- 
Rob
__________________________________________________
Delphi-Talk mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi-talk

Reply via email to