Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Open a text file in WRITE-ONLY mode

Posted on 1998-02-07
3
Medium Priority
?
188 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 100 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

Technology Partners: 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

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

926 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