File Types in Delphi

Source: Internet
Author: User
File Types in Delphi

By clin003 at 20070528 from: http://blog.csdn.net/clin003

A file is an ordered collection of the same type of elements and a channel for data transmission between memory and peripherals. Some peripherals, such as monitors, keyboards, printers, and so on, can be seen as files, but the most commonly used is magnetic

Disk Files, which we will discuss in this lesson.

Delphi inherits the file management function of Object Pascal and has made great development. The most important thing is to provide standard controls for file management and

Multiple file management functions.
First, we will introduce the basic concepts and standard procedures/functions of Delphi file management, and provide an application instance for recording files. Next, we will introduce how to use the file control provided by Delphi.

Method.

6.1 file types and standard processes

As with Object Pascal, Delphi supports three file types: text files, type files, and non-type files.

6.1.1 text file

Variables of the text file type are declared as follows:

VaR

Textfilevar: text;

Text files are read and written in the unit of behavior. Because the length of each row is not necessarily the same, the exact position of the given row in the file cannot be calculated, so the row can only be read and written in sequence.

. In addition, a text file can only be opened for reading or writing. Simultaneous read and write operations on an opened text file are not allowed.

6.1.1.1 open and close a text file

To open a text file, two steps are required: (1). The file variables are associated with the file name; (2). initialize read/write.

The assignfile standard process for associating file variables and file names:

Assignfile (textfilevar, 'G:/g _/Delphi/top.3800hk.com/7th Lesson/.txt ');

Filename can be either a full path name or a file name. The latter will be searched in the current directory.

Assignfile is a new function provided by Delphi. Its function is equivalent to assign in Object Pascal. In Delphi, assign is more used as a method name.

There are three methods to initialize read/write:

1. Reset: open the file for reading and move the file pointer to the beginning of the file;

2. Rewrite: Create a new file for the write operation;

3. append: open an existing file for writing and locate the file pointer at the end of the file.

When the reset or append process is used and the file does not exist, an I/O exception is thrown.

Closing a file is simple. You only need to call the closefile process.

Although the Delphi application automatically closes all opened files when it exits, You can manually close the file to ensure that the file handle is released and the portability of the program is enhanced.

To maintain compatibility, Delphi also allows you to use assign to establish an association and close the file.

Example code:
VaR
Mytextfile: textfile;
Begin
Assignfile(mytextfile,'mytextfile.txt ');
Reset (mytextfile );
// Rewrite (mytextfile );
// Append (mytextfile );
Try
{File Operations}
Finally
Closefile (mytextfile );
End;
End;

///////////////////////////

Read/write of text files

The read and readln standard processes are used to read information from text files.

When reading a value, read and readln assume that the value is separated by one or more spaces, rather than commas, semicolons, or other characters. For the following statement:

Read (textfilevar, num1, num2, num3 );

If the value in the file is:

100 200 300

It can be read successfully, and if the value in the file is

100 200,300

When read reads "200," and tries to convert it into a value, an exception is thrown.

When the read character is a string, the read and readln processes always read as many characters as possible to fill in the string variable or until the row Terminator is read. Therefore, to read formatted string data from a text file, you must declare a string variable that matches its length. To read words from a file, you must first read each line in the file into the string, and then analyze words one by one from the string. Or read only one character from a text file at a time and test whether the word is broken after each character.

The delimiter between the formatted strings should be read into a temporary variable, and the delimiter between the string and the value, the value and the value will be automatically recognized and removed when the Delimiter is read. For the following row of data:

Mon 40 50

Definition

VaR

Day: String [3];

Time: String [5];

Num1, num2: integer;

You must use the following read statement to read data:

Read (textfilevar, day, C, time, num1, num2 );

C is a temporary character variable.

DEMO code:

{VaR
Mytextfile: textfile;
Day: String [3];
Time: String [5];
Num1, num2: integer;
C: Char;
Begin
Assignfile (mytextfile, 'G:/g _/Delphi/top.3800hk.com/7th Lesson /pl/mytextfile.txt ');
Reset (mytextfile );
// Rewrite (mytextfile );
// Append (mytextfile );
Try
{File Operations}
Read (mytextfile, day, C, time, num1, num2 );

Finally
Closefile (mytextfile );
End;

Showmessage ('day is: '+ day + 'Time is:' + time + 'and the orther num is:' + inttostr (num1) + ''+ inttostr (num2 ));
End ;}

Edit a text file

To edit a text file in Delphi, you only need to associate it with a tmemo control:

Memo1.lines. loadfromfile (textfilename );

All modifications made on tmemo will be reflected in the file after the savetofile method of the memo control is called.
// Memo1.lines. loadfromfile (textfilename); read (load) the file.
Memo1.lines. savetofile (textfilename); save the file.
////////////////////////////

By clin003 at 20070528 from: http://blog.csdn.net/clin003
6.1.2 type file

Record Files are more flexible file types. It can be opened for both reading and writing, and can be accessed randomly because the length of each record in the record file is fixed.

The type variable of the record file can be declared as follows:
Type
Tperson = packed record
Firstname: String [20];
Lastname: String [20];
Age: integer;
End;

VaR

Recordfilevar: file of tperson;

Recordtype is a custom record type.

Sample Code:
VaR
Personfile: file of tperson;
Person: tperson;
Begin
Assignfile (personfile, 'personfile. dat ');
Reset (personfile );
Try
If not EOF (personfile) then
Read (personfile, person );
Finally
Closefile (personfile );
End;
End;

6.1.3 untyped files

Untyped files provide underlying I/O channels for accessing files with variable length records. It is often used in file copy operations. Because delphi provides better methods,
Therefore, untyped files are rarely used. Interested readers can refer to the online help topics blockread and blockwrite.
Non-type file declaration:
VaR
Untypedfile: file;
Buf: array [0 .. 128] of byte;
In this way, it is declared to pull a file consisting of a data block sequence. Each data block is 128 bytes of data.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.