From: "Peter Below (TeamB)" <100113.1101@compuXXserve.com> Subject: Re: How to read and write textfile at the same time? Date: 24 Jun 1999 00:00:00 GMT Message-ID: Content-Transfer-Encoding: 8bit References: <7ksdtd$c261@forums.borland.com> Content-Type: text/plain; charset=iso-8859-1 Organization: TeamB Mime-Version: 1.0 Reply-To: 100113.1101@compuXXserve.com Newsgroups: borland.public.delphi.objectpascal > My problem is that I need to read a text file (from a network drive) with my > delphi program and at the same time another program must be able to write > into this text file. > So far I've tried to use the assignfile&reser method. But when I use the > assignfile with a variable that is defined as a textfile another programs > cannot write to this textfile. Jarno, you cannot use a Delphi Textfile variable for this since Delphi always opens these files in exclusive mode, since they are internally buffered. You have to use a TFileStream. Here is an example that fakes you situation using two timers, on the first timers events the program writes text to the shared file, on the second timers events it reads text from the file and displays it in a memo. The timers can be individually started and stopped. unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Buttons; type TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; Button2: TButton; WriterTimer: TTimer; ReaderTimer: TTimer; procedure FormCreate(Sender: TObject); procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure WriterTimerTimer(Sender: TObject); procedure ReaderTimerTimer(Sender: TObject); private { Private declarations } FFilename: String; FReaderStream, FWriterStream: TFileStream; public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.FormCreate(Sender: TObject); var buf: array[0..MAX_PATH] of Char; begin GetTempPath( Sizeof(buf), buf ); FFilename := buf + 'test.log'; If FileExists( FFilename ) Then DeleteFile( FFilename ); end; procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean); begin FReaderStream.Free; FReaderStream := Nil; FWriterStream.Free; FWriterStream := Nil; end; procedure TForm1.Button1Click(Sender: TObject); begin // start or stop the writer If WriterTimer.Enabled Then Begin // stop the writer WriterTimer.Enabled := False; FWriterStream.Free; FWriterStream := Nil; (Sender As TButton).Caption := 'Start writer'; End Else Begin // start the writer If not FileExists( FFilename ) Then With TFileStream.Create( FFilename, fmCreate ) Do Free; FwriterStream:= TFileStream.Create( FFilename, fmOpenWrite or fmShareDenyNone ); With FwriterStream Do Position := Size; // always append to the file (Sender As TButton).Caption := 'Stop writer'; WriterTimer.Enabled := True; End; end; procedure TForm1.Button2Click(Sender: TObject); begin // start or stop the reader If ReaderTimer.Enabled Then Begin // stop the reader ReaderTimer.Enabled := False; FReaderStream.Free; FReaderStream := Nil; (Sender As TButton).Caption := 'Start reader'; End Else Begin // start the reader If not FileExists( FFilename ) Then With TFileStream.Create( FFilename, fmCreate ) Do Free; FReaderStream:= TFileStream.Create( FFilename, fmOpenRead or fmShareDenyNone ); // start reading always at position 0. (Sender As TButton).Caption := 'Stop reader'; memo1.clear; ReaderTimer.Enabled := True; End; end; procedure TForm1.WriterTimerTimer(Sender: TObject); var S: String; begin If Assigned( FwriterStream ) Then Begin S:= Format('Line written at tickcount %d'#13#10, [GetTickCount] ); try FWriterStream.WriteBuffer( S[1], Length(S) ); except Button1.Click; raise end; End; end; procedure TForm1.ReaderTimerTimer(Sender: TObject); var len: Integer; S: String; begin If Assigned( FReaderStream ) Then Begin With FReaderStream Do len := Size - Position; If len > 0 Then Begin SetLength( S, len ); try FReaderStream.ReadBuffer( S[1], len ); except Button2.Click; raise; end; { except } memo1.selstart := memo1.gettextlen; memo1.Seltext := Format('%d characters read at tickcount %d'#13#10'%s', [ len, GetTickCount, S ]); memo1.perform( em_scrollcaret, 0, 0); End; End; end; end. { object Form1: TForm1 Left = 192 Top = 128 Width = 480 Height = 480 Caption = 'Form1' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -13 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False OnCloseQuery = FormCloseQuery OnCreate = FormCreate PixelsPerInch = 120 TextHeight = 16 object Button1: TButton Left = 28 Top = 12 Width = 85 Height = 25 Caption = 'Start writer' TabOrder = 0 OnClick = Button1Click end object Memo1: TMemo Left = 36 Top = 48 Width = 393 Height = 373 Lines.Strings = ( 'Memo1') ScrollBars = ssVertical TabOrder = 1 end object Button2: TButton Left = 116 Top = 12 Width = 85 Height = 25 Caption = 'Start reader' TabOrder = 2 OnClick = Button2Click end object WriterTimer: TTimer Enabled = False Interval = 200 OnTimer = WriterTimerTimer Left = 212 Top = 8 end object ReaderTimer: TTimer Enabled = False Interval = 500 OnTimer = ReaderTimerTimer Left = 252 Top = 8 end end } Peter Below (TeamB) 100113.1101@compuserve.com) No e-mail responses, please, unless explicitly requested!