Solved

Open a text file in WRITE-ONLY mode

Posted on 1998-02-07
3
178 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now