Examples Delphi

READING AND WRITING OF LONG INTEGER VALUES TO FILE
(example using [File of LongInt])
WHOLE PROGRAM:
unit FileUnit;
{Richard E. -small program to test the writing and
reading of files of Integer, LongInt etc..}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
const
maxLongSize = 100000000;
inFileName = 'LongFile.TMP';
outFileName = 'LongFile.TMP';
arrayLen = 60;
type
TForm1 = class(TForm)
LoadButton: TButton;
ExitButton: TButton;
SaveButton: TButton;
procedure ExitButtonClick(Sender: TObject);
procedure SaveButtonClick(Sender: TObject);
procedure LoadButtonClick(Sender: TObject);
function GetError (const ErrorCode: Integer): String;
procedure WriteLongArray;
procedure ReadIntoLongArray;
procedure FormCreate(Sender: TObject);
private
{private declarations}
srcFile, destFile: File of LongInt;
longArray: array[0..arrayLen] of LongInt;
tempLongArray: array[0..arrayLen] of LongInt;
public
{public declarations}
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.ExitButtonClick(Sender: TObject);
begin
Close;
end;
procedure TForm1.SaveButtonClick(Sender: TObject);
begin
AssignFile(destFile, outFileName);
try {to open destination file}
reWrite(destFile);
try
WriteLongArray;
finally
closefile(destFile)
end;
except
on E: EInOutError do
begin
MessageDlg('Error reading ' + uppercase(outFileName) + '.'#13 +
GetError(E.ErrorCode)+'.', mterror,[mbOK], 0);
end
end; {trying to open destination file}
end;
procedure TForm1.LoadButtonClick(Sender: TObject);
begin
AssignFile(srcFile, inFileName);
try {to open source file}
reSet(srcFile);
try
ReadIntoLongArray;
finally
closefile(srcFile)
end; {putting source file data into memory}
except
on E: EInOutError do
begin
MessageDlg('Error reading ' + uppercase(inFileName) + '.'#13 +
GetError(E.ErrorCode)+'.', mterror,[mbOK], 0);
end
end; {trying to open source file}
end;
function TForm1.GetError (const ErrorCode: integer): string;
{returns a string pertaining to the type of error. If IO-checking was off
we could check for errors by looking at IOResult, but in this program we
use an exception handler (in the file reading routine (above). The
strings listed below are taken from Borland's 'Object Pascal Language Guide'
for Delphi Version 1.0, pages 273-275...}
begin
case ErrorCode of
2: Result := 'File not found';
3: Result := 'Path not found';
4: Result := 'Too many open files';
5: Result := 'File access denied';
6: Result := 'Invalid file handle';
12: Result := 'Invalid file access code';
15: Result := 'Invalid drive';
100: Result := 'Disk read error';
101: Result := 'Disk write error';
102: Result := 'File not assigned';
103: Result := 'File not open';
else
Result := ''
end
end;
procedure TForm1.WriteLongArray;
{write to source file...}
var
i: Integer;
begin
for i := 0 to arrayLen do
begin
Write(destFile, longArray[i]);
end;
end;
procedure TForm1.ReadIntoLongArray;
{read from source file...}
var
i: Integer;
begin
for i := 0 to arrayLen do
begin
Read(srcFile, tempLongArray[i]);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
randomLong: LongInt;
begin
Randomize;
for i := 0 to arrayLen do
begin
randomLong := Round(Random(maxLongSize));
longArray[i] := randomLong;
end;
end;
end.
*************************************************************************
READING AND WRITING OF LONG INTEGER VALUES TO FILE
(example using a text file with conversion code...)
WHOLE PROGRAM:
unit FileUnit;
{Richard E. -small program to test the writing and
reading of files of Integer, LongInt etc..}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
const
maxLongSize = 100000000;
inFileName = 'LongFile.TMP';
outFileName = 'LongFile.TMP';
arrayLen = 60;
type
TForm1 = class(TForm)
LoadButton: TButton;
ExitButton: TButton;
SaveButton: TButton;
procedure ExitButtonClick(Sender: TObject);
procedure SaveButtonClick(Sender: TObject);
procedure LoadButtonClick(Sender: TObject);
function GetError (const ErrorCode: Integer): String;
procedure WriteLongArray;
procedure ReadIntoLongArray;
procedure FormCreate(Sender: TObject);
private
{private declarations}
srcFile, destFile: TextFile;
longArray: array[0..arrayLen] of LongInt;
tempLongArray: array[0..arrayLen] of LongInt;
public
{public declarations}
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.ExitButtonClick(Sender: TObject);
begin
Close;
end;
procedure TForm1.SaveButtonClick(Sender: TObject);
begin
AssignFile(destFile, outFileName);
try {to open destination file}
reWrite(destFile);
try
WriteLongArray;
finally
closefile(destFile)
end;
except
on E: EInOutError do
begin
MessageDlg('Error reading ' + uppercase(outFileName) + '.'#13 +
GetError(E.ErrorCode)+'.', mterror,[mbOK], 0);
end
end; {trying to open destination file}
end;
procedure TForm1.LoadButtonClick(Sender: TObject);
begin
AssignFile(srcFile, inFileName);
try {to open source file}
reSet(srcFile);
try
ReadIntoLongArray;
finally
closefile(srcFile)
end; {putting source file data into memory}
except
on E: EInOutError do
begin
MessageDlg('Error reading ' + uppercase(inFileName) + '.'#13 +
GetError(E.ErrorCode)+'.', mterror,[mbOK], 0);
end
end; {trying to open source file}
end;
function TForm1.GetError (const ErrorCode: integer): string;
{returns a string pertaining to the type of error. If IO-checking was off
we could check for errors by looking at IOResult, but in this program we
use an exception handler (in the file reading routine (above). The
strings listed below are taken from Borland's 'Object Pascal Language Guide'
for Delphi Version 1.0, pages 273-275...}
begin
case ErrorCode of
2: Result := 'File not found';
3: Result := 'Path not found';
4: Result := 'Too many open files';
5: Result := 'File access denied';
6: Result := 'Invalid file handle';
12: Result := 'Invalid file access code';
15: Result := 'Invalid drive';
100: Result := 'Disk read error';
101: Result := 'Disk write error';
102: Result := 'File not assigned';
103: Result := 'File not open';
else
Result := ''
end
end;
procedure TForm1.WriteLongArray;
{write to source file...}
var
i: Integer;
tempString: String;
begin
for i := 0 to arrayLen do
begin
tempString := IntToStr(longArray[i]);
Write(destFile, tempString);
Write(destFile, ',');
end;
end;
procedure TForm1.ReadIntoLongArray;
{read from source file...}
const
thisArrayLen = 100;
var
i: Integer;
tempArray: array[0..thisArrayLen] of Char;
tempString: String;
tempLong: LongInt;
ch: Char;
begin
i := 0;
while not(EOF(srcFile)) do
begin
repeat
Read(srcFile, ch);
tempArray[i] := ch;
Inc(i);
until ((ch = ',') or (EOF(srcFile)));
tempArray[i - 1] := #0;
tempString := String(tempArray);
tempLong := StrToInt(tempString);
i := 0;
Read(srcFile, ch);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
randomLong: LongInt;
begin
Randomize;
for i := 0 to arrayLen do
begin
randomLong := Round(Random(maxLongSize));
longArray[i] := randomLong;
end;
end;
end.