在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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. 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.
Note: To create a typed file in Delphi, we use the following syntax:
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.
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:
Read from a File In order to retreive all the information from the 'members.dat' file we could use this code:
Seeking and PositioningFiles 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.
Change and UpdateWe 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:
What's important is that this file is not an ASCII file, this is how it looks in Notepad (only one record):
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论