Solved

Open a text file in WRITE-ONLY mode

Posted on 1998-02-07
3
181 Views
Last Modified: 2010-04-04
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.

0
Comment
Question by:Kapusta
  • 2
3 Comments
 
LVL 5

Expert Comment

by:ronit051397
ID: 1358568
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.}

0
 

Author Comment

by:Kapusta
ID: 1358569
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;
0
 
LVL 5

Accepted Solution

by:
ronit051397 earned 50 total points
ID: 1358570
You tend to reject answers immediately, without checking further
(See TMediaPlayer question...)

My solution work. The problem is with your code: You cannot pass the file handle parameter to the Append or Rewrite method (See Delphi Help).
If you are using API's, then you should be consistent with that.
Here is a sample, not using the API's directly.
This code was tested under WinNT4.0 with Delphi 3, but not with NOVELL.

procedure TForm1.Button1Click(Sender: TObject);
var
  LogFileHandle : Integer;
  Path, LogFileName, FString : String;
  Buf: PChar;
begin
  if InputQuery('sample','Enter Text to add to the file',FString) then
    if FString<>'' then
  begin
    GetMem(Buf, Length(FString)+1+2);//2 for the #13#10
    Buf[0]:=#0;
    StrPCopy(Buf, FString);
    StrCat(Buf, #13#10);//to write in a new line
    if LogfileName = '' then LogfileName := 'RECORD.LOG';
    if FileExists(Path + LogfileName) then
      LogFileHandle := FileOpen(Path + LogFileName, fmOpenWrite)//opening only for writing
    else
      LogFileHandle := FileCreate(Path + LogFileName);//when we create a file, it's empty, so there will be no "reading"
    FileSeek(LogFileHandle,0,2);//move the curser to the end of the file in order to append the buffer
    FileWrite(LogFileHandle,  Buf^, StrLen(Buf));
    FileClose(LogFileHandle);
    FreeMem(Buf);
  end;
end;

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Communication Between RC4 Delphi <-> PHP 3 123
How to call a form that is in a DLL  from an application? 13 81
DBGrid or StringGrid ? 6 104
update joined tables 2 55
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question