• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

CreateyourownDatabaseusingDelphi's"FileOf"TypedFiles

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Files

Simply put a file is a binary sequence of some type. In Delphi there a three classes of file:typed, text, and untyped. Typed files are files that contain data of a particular type, such as Double, Integer or previously defined custom Record type. Text files contain readable ASCII characters. Untyped files are used when we want to impose the least possible structure on a file.

Typed Files

While text files consist of lines terminated with a CR/LF (#13#10)combination, typed files consist of data taken from a particular type of data structure.

For example, the following declaration creates a record type called TMember and an array of TMember record variables.

 type
   TMember = record
     Name : string[50];
     eMail : string[30];
     Posts : LongInt;
   end;
 
  var Members : array[1..50] of TMember; 
Before we can write the information to the disk we have to declare a variable of a file type. The following line of code declares an F file variable.
 var F : file of TMember; 

Note: To create a typed file in Delphi, we use the following syntax:

var SomeTypedFile : file of SomeType

In order to start working with files from Delphi we have to link a file on a disk to a file variable in our program. To create this link we must use AssignFile procedure in order to associate a file on a disk with a file variable.

 AssignFile(F, 'Members.dat') 
 
Write to a File

Suppose we have filled an array of Delphi members with their names, e-mails and number of posts and we want to store this information in a file on the disk. The following peace of code will do the work:

 var
   F : file of TMember;
   i : integer;
 begin
  AssignFile(F,'members.dat') ;
  Rewrite(F) ;
  try
   for j:= 1 to 50 do
    Write (F, Members[j]) ;
  finally
   CloseFile(F) ;
  end;
 end; 

Read from a File

In order to retreive all the information from the 'members.dat' file we could use this code:

 var
   Member: TMember
   F : file of TMember;
 begin
  AssignFile(F,'members.dat') ;
  Reset(F) ;
  try
   while not Eof(F) do begin
    Read (F, Member) ;
    {DoSomethingWithMember;}
   end;
  finally
   CloseFile(F) ;
  end;
 end; 
 
Seeking and Positioning
Files are normally accessed sequentially. When a file is read using the standard procedure Read or written using the standard procedure Write, the current file position moves to the next numerically ordered file component (next record). Typed files files can also be accessed randomly through the standard procedure Seek, which moves the current file position to a specified component. The FilePos and FileSize functions can be used to determine the current file position and the current file size.
 {go back to the beginning - the first record}
 Seek(F, 0) ;
 
 {go to the 5-th record}
 Seek(F, 5) ;
 
 {Jump to the end - "after" the last record}
 Seek(F, FileSize(F)) ; 
Change and Update
We just learned how to write and read the entire array of Members. What when all we want to do is to seek to the 10-th member and change his e-mail? The next procedure does exactly that:
 procedure ChangeEMail(const RecN : integer; const NewEMail : string) ;
 var DummyMember : TMember;
 begin
  {assign, open, exception handling block}
  Seek(F, RecN) ;
  Read(F, DummyMember) ;
  DummyMember.Email := NewEMail;
  {read moves to the next record, we have to
  go back to the original record, then write}
  Seek(F, RecN) ;
  Write(F, DummyMember) ;
  {close file}
 end;
 
What's important is that this file is not an ASCII file, this is how it looks in Notepad (only one record):
 .Delphi Guide g Ò5·¿ì. 5. . B V.Lƒ ,„¨[email protected]Ï.. ç.ç.ï.. 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Matlab高级教程_第三篇:Matlab转码C/C++方式(混编)_第一部分发布时间:2022-07-18
下一篇:
DelphiTcxTreeList总结发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap