Solved

Open a text file in WRITE-ONLY mode

Posted on 1998-02-07
3
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
LAN or WAN ? 11 106
Is anyone willing to have a look through this code and help debug? 25 93
Wincontrol not (correctly) drawn 15 48
Delphi: making a BW image transparent 10 94
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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 an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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