home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Delphi Programming Unleashed
/
Delphi_Programming_Unleashed_SAMS_Publishing_1995.iso
/
chap14
/
easyfile
/
main.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1995-03-20
|
3KB
|
133 lines
unit Main;
{ Program copyright (c) 1995 by Charles Calvert }
{ Project Name: EASYFILE }
{
This program shows how to work with strings and
with text based files.
Constants for using with the MODE field
of TTextRec, declared in SYSUTILS.PAS :
fmClosed = $D7B0;
fmInput = $D7B1;
fmOutput = $D7B2;
fmInOut = $D7B3;
This program gives a general workout on
file IO issues. In particular, it shows
how to use the TTextRec structure to determine
if a file is open, closed, etc.
As declared in SYSUTILS:
PTextBuf = ^TTextBuf;
TTextBuf = array[0..127] of Char;
TTextRec = record
Handle: Word;
Mode: Word;
BufSize: Word;
Private: Word;
BufPos: Word;
BufEnd: Word;
BufPtr: PTextBuf;
OpenFunc: Pointer;
InOutFunc: Pointer;
FlushFunc: Pointer;
CloseFunc: Pointer;
UserData: array[1..16] of Byte;
Name: array[0..79] of Char;
Buffer: TTextBuf;
end;
}
interface
uses
WinTypes, WinProcs, Classes,
Graphics, Forms, Controls,
StdCtrls, SysUtils, ExtCtrls,
Dialogs;
type
TForm1 = class(TForm)
RunTest: TButton;
OpenInput: TButton;
OpenOutPut: TButton;
CloseFile: TButton;
Panel1: TPanel;
Panel2: TPanel;
Label1: TLabel;
Label2: TLabel;
procedure RunTestClick(Sender: TObject);
procedure OpenInputClick(Sender: TObject);
procedure OpenOutPutClick(Sender: TObject);
procedure CloseFileClick(Sender: TObject);
private
F: System.Text;
end;
var
Form1: TForm1;
implementation
uses
StrBox;
{$R *.DFM}
function GetMode(var F: Text): string;
begin
case TTextRec(F).Mode of
fmClosed: Result := 'Closed';
fmInput: Result := 'Open for Input';
fmOutPut: Result := 'Open for Output';
fmInOut: Result := 'Open for input and output';
end;
end;
procedure TForm1.RunTestClick(Sender: TObject);
var
i: Integer;
begin
System.Assign(F, GetTodayName('EZ', 'txt'));
Label1.Caption := TTextRec(F).Name;
Label2.Caption := GetMode(F);
for i := 0 to ComponentCount - 1 do
if Components[i] is TButton then
TButton(Components[i]).Enabled := True;
end;
{ Exception handling is explained in the
chapter entitled "Exceptions". The
exception will be raised if a file
with the filename generated above
does not exist. }
procedure TForm1.OpenInputClick(Sender: TObject);
begin
try
Reset(F);
except
on EInOutError do
MessageDlg('File must be created first', mtInformation, [mbOk], 0);
end;
Label2.Caption := GetMode(F);
end;
procedure TForm1.OpenOutPutClick(Sender: TObject);
begin
ReWrite(F);
Label2.Caption := GetMode(F);
end;
procedure TForm1.CloseFileClick(Sender: TObject);
begin
if TTextRec(F).Mode <> fmClosed then
System.Close(F);
Label2.Caption := GetMode(F);
end;
end.