Link to home
Start Free TrialLog in
Avatar of Kapusta
Kapusta

asked on

Open a text file in WRITE-ONLY mode

Is it possible to open a Text file for WRITE-ONLY access.  I do NOT want READ and WRITE access, but rather just WRITE-ONLY access.  I am using Delphi 1.

I normally use the following routine for creating/appending text files:

var
 LogFileHandle : System.Text;
 Path , Logfilename : String;
...
begin
...
if LogfileName = '' then LogfileName := 'RECORD.LOG';
System.Assign(LogFileHandle, Path + LogFileName);
try
      if FileExists(Path + LogfileName) then
            Append(LogFileHandle)
      else
            Rewrite(LogFileHandle);
      {end if}
except
      on E:Exception do begin
            ShowMessage(E.Message); {show the user what the error was}
      System.Close(LogFileHandle);
      end;
end;

 The file in the example above will be written/created on a Novell network where the directory (Path) will NOT have READ access rights...only WRITE privileges.

Avatar of ronit051397
ronit051397

procedure TForm1.Button1Click(Sender: TObject);
var FileHandle: THandle;
begin
  FileHandle:=_lopen('Some path', OF_WRITE);
{  ... do something}
  _lclose(FileHandle);
end;


{for more information see the  File Functions in API help.}

Avatar of Kapusta

ASKER

Sorry, but your solution does not work.  It does not allow me to write text to ASCII text files.

This function -->   Append(LogFileHandle) will generate an ERROR 77 (File variable expected).

Here's the modified code based upon your suggestion:

var
  MyPChar : array[0..255] of Char;
  LogFileHandle : THandle;
  Path, LogFileName : String
begin
...
if LogfileName = '' then LogfileName := 'RECORD.LOG';

{System.Assign(LogFileHandle, ReadFromAlternatePath + LogFileName);}

LogFileHandle := _lopen(StrPCopy(MyPChar,Path + LogFileName), OF_WRITE);

try
 if FileExists(Path + LogfileName) then
   Append(LogFileHandle)
 else
   Rewrite(LogFileHandle);
 {end if}
except
  on E:Exception do begin
    ShowMessage(E.Message); {show the user what the error was}
{System.Close(LogFileHandle);}
 _lclose(LogFileHandle);
end;
ASKER CERTIFIED SOLUTION
Avatar of ronit051397
ronit051397

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial