Open file for append when exists else create new one

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Open file for append when exists else create new one



I want to achieve this:

If file with given name already exists then open it for append else create new one and open it for append.

I use something like:

  if FileExists(FileName) then F:=FileOpen(FileName, fmOpenWrite+fmShareDenyWrite) else F:=FileCreate(FileName);
  FileSeek(F, 0, 2); //... 0 bytes from the end of the file
  FileWrite(F, Pointer(s)^, length(s));


  if FileExists(FileName) then
    FS := TFileStream.Create(FileName, fmOpenWrite+fmShareDenyWrite)
    FS := TFileStream.Create(FileName, fmCreate+fmShareDenyWrite);
  FS.Seek(0, soEnd);
  FS.Write(s[1], Length(s));

Is there way how to achieve this using one-line without checking FileExists ? i.e.:
resulting efect like when using on Windows CreateFile(FileName, GENERIC_WRITE, FILE_SHARE_READ, OPEN_ALWAYS, ...)
CreationDisposition =  OPEN_ALWAYS - Opens a file, always if exists else create new one.
If the specified file exists, the function succeeds and the last-error code is set to ERROR_ALREADY_EXISTS (183).
If the specified file does not exist and is a valid path to a writable location, the function creates a file and the last-error code is set to zero.


fpc-pascal maillist  -  [hidden email]