Solved

Open a text file in WRITE-ONLY mode

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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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