wimhhf
asked on
Header in Untyped Files
Hello,
Does anybody know how you can make a 'header' in untyped files?
It's usual to store information in untyped files of the same type, which is specific beforehand. But is it possible to store other information? (example: a header)
What's the difference between typed and untyped files?
I can't find some information about that in Delphi Help..
Greetings,
Wim
Does anybody know how you can make a 'header' in untyped files?
It's usual to store information in untyped files of the same type, which is specific beforehand. But is it possible to store other information? (example: a header)
What's the difference between typed and untyped files?
I can't find some information about that in Delphi Help..
Greetings,
Wim
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
hi wimhhf;
Your header:
Type
THeader = Record
ID : String[4];
Version : Word;
Author : String[30];
End;
VAR
myHeader : tHeader;
f : File;
Begin
// Init. your Header
AssignFile(f,myHeader );
Rewrite(f,1);
BlockWrite ( f,Header,SizeOf(Header));
// Read other Blocks
CloseFile(f);
End.
The difference between typed and untyped files:
Typed Files:
Example :
TAdr = Record
Name : String[30];
Tele : String[30];
End;
VAR
F : File OF TADR;
Rec : TAdr;
To Read an Addres form the File Read ( f, Rec);
To Write an Addres to the file ( f, Rec);
Best Regards
Cesario
Your header:
Type
THeader = Record
ID : String[4];
Version : Word;
Author : String[30];
End;
VAR
myHeader : tHeader;
f : File;
Begin
// Init. your Header
AssignFile(f,myHeader );
Rewrite(f,1);
BlockWrite ( f,Header,SizeOf(Header));
// Read other Blocks
CloseFile(f);
End.
The difference between typed and untyped files:
Typed Files:
Example :
TAdr = Record
Name : String[30];
Tele : String[30];
End;
VAR
F : File OF TADR;
Rec : TAdr;
To Read an Addres form the File Read ( f, Rec);
To Write an Addres to the file ( f, Rec);
Best Regards
Cesario
u can use a typed file and use the first record for the header. This is how I do. I think its the easyest way
ASKER
Thanks for your detailed answer, but I have still a question.
I have 2 records:
TPerson = record
ID: LongInt;
Name: string[15];
Phone: string[10];
end;
TOther = record
Owner = string[20];
Date = string[20];
Count = LongInt;
end;
Is it possible to make a typed file where I store records of TPerson.
But in the beginning of the typed file I put a record of TOther (like a header).
or in a different way:
Is it possible to make a untyped file where I store records of TPerson and other records/integer/strings.
But I don't understand exactly how to read/write the records and the strings like I do in a typed file...
In short: I'd like to store records in a file, but I'd to store also other variables in the same file. I know it is possible, but how to do is a bit fuzzy for me.
Greetings,
Wim
I have 2 records:
TPerson = record
ID: LongInt;
Name: string[15];
Phone: string[10];
end;
TOther = record
Owner = string[20];
Date = string[20];
Count = LongInt;
end;
Is it possible to make a typed file where I store records of TPerson.
But in the beginning of the typed file I put a record of TOther (like a header).
or in a different way:
Is it possible to make a untyped file where I store records of TPerson and other records/integer/strings.
But I don't understand exactly how to read/write the records and the strings like I do in a typed file...
In short: I'd like to store records in a file, but I'd to store also other variables in the same file. I know it is possible, but how to do is a bit fuzzy for me.
Greetings,
Wim
You can do this:
var
Person: arra [1 .. 20] of TPerson;
Other: TOther;
F: file;
i: Integer;
begin
Assign..
Rewrite(F, 1);
// Write other as a header:
BlockWrite(F, Other, SizeOf(Other));
// Write persons:
for i:= 1 to 20 do
BlockWrite(F, Person[i], SizeOf(TPerson));
CloseFile(F);
end;
to read:
BlockRead(F, Other, SizeOf(Other), NumRead);
i:= 0;
while not eof (F) do
begin
Inc(i);
BlockRead(F, Person[i], SizeOf(TPerson), NumRead);
// Display the records
end;
CloseFile(F);
I hope you mean this
Motaz
var
Person: arra [1 .. 20] of TPerson;
Other: TOther;
F: file;
i: Integer;
begin
Assign..
Rewrite(F, 1);
// Write other as a header:
BlockWrite(F, Other, SizeOf(Other));
// Write persons:
for i:= 1 to 20 do
BlockWrite(F, Person[i], SizeOf(TPerson));
CloseFile(F);
end;
to read:
BlockRead(F, Other, SizeOf(Other), NumRead);
i:= 0;
while not eof (F) do
begin
Inc(i);
BlockRead(F, Person[i], SizeOf(TPerson), NumRead);
// Display the records
end;
CloseFile(F);
I hope you mean this
Motaz
ASKER
Thanks!
------------------
Typed files or data files used to store homogeneous data such as bytes, integers, records, user-defiened types, and other Object Pascal types.
Typed files in Object Pascal
Typed files in Object Pascal declaration depends on record type but the main structure of declaration is:
var
F: file of <Type>;
Type can be any object Pascal type but not Object such as:
var
IntF: file of integer;
CharF: file of char;
RecF: file of TRec;
The most important procedures and functions used with typed files are below:
--------------------------
Operation proc/func
--------------------------
Declaration var F: file of <Type>;
Assigning AssignFile(F);
Opening for reading Reset(F); or Rewrite(F);
Opening for writing Rewrite(F) or Reset(F) with FileMode = 2
Opening for read/write Reset(F) with FileMode = 2
Appending FileMode:= 2; Reset(F); Seek(F, FileSize(F));
Reading Read(F, X); { X is a variable of same file type }
Writing Writ(F, X);
Goto random position Seek(F, Position); { Position is a number of desired record }
Closing file CloseFile(F);
Getting records count FileSize(F);
Getting current position FilePos(F);
--------------------------
Typed file is organized as random access file so that you can open it in reading/writing mode and you can goto any record directly using Seek procedure.
Reset in typed file opens file for reading, writing, or reading and writing mode according to FileMode variable:
If FileMode = 0 that means read only
If FileMode = 1 that means write only
If FileMode = 2 that means read and write access
Befor openning file with reset make sure that the file is exists.
Rewrite in typed file create a new file and it overwrite the file if it is already exists. When you create file with Rewrite you can read and write from and to file.
Append does not work with typed files, insead you must open file with reset and goto beyond last record such as:
FileMode:= 2; // Read and write mode
Reset(F);
Seek(F, FileSize(F));
If file size if 5 that means there is 5 records in file from 0 to 4 so that going to record number 5 will ensures writing on an empty record. This works only if file exists but it will rais an exception if file does not exists, so that you must write:
AssignFile(F, FileName);
FileMode:= 2; // Read and write mode
if FileExists(FileName) then
Reset(F)
else
Rewrite(F); // Create new file
Seek(F, FileSize(F));
Example:
(********* Save data **********)
procedure SaveData;
var
F: file of integer;
Data: integer;
begin
AssignFile(F, 'Test.Dat');
Rewrite(F);
Repeat
Data:= StrToInt(InputBox('Data entry',
'Enter any numbers, enter 0 to stop data entry', ''));
Write(F, Data);
until Data = 0;
CloseFile(F);
end;
(********* Read data **********)
procedure ReadData;
var
F: file of integer;
Data: integer;
begin
AssignFile(F, 'Test.Dat');
FileMode:= 0; // Read only
Reset(F);
while not Eof(F) do
begin
Read(F, Data);
ShowMessage(IntToStr(Data)
end; // while
CloseFile(F);
end;
Notes:
- If you want to open typed file for reading you must set FileMode to 0 because if you do not assign 0 to FileMode the default value of it will be 2 which means read and write access. If you open a read-only file or a file in CD-ROM and you want to read it, openning it with reset means read and write if FileMode = 2 which will generates access denied error and prevent you from reading file.
- Records in typed files have a fixed length, so that it does not require any seperator, example if you write three records in a typed file and record size is 10 bytes, actual file size will be 30.
- You can not write long string in typed file, instead you have to determine it's size such as:
type
TLine = String[30];
var
F: file of TLine;
Line: TLine;
---------------
Motaz