The file is a sequence of elements of the same type are arranged sequentially. The data files are stored in external memory under a certain name.

Bạn đang xem: Làm việc với tệp trong pascal

File aggregation in it some element of data has the same structure but different array lượt thích array element number of the file is not defined.
1. The text file:Used to store data in the form of characters ASCII, these characters are saved each line, length of the line may vary. Example 2008 (type word) they are written to lớn a text file to 4 Byte (not 2 Byte).
2. File type: The tệp tin is a file type that its elements have the same length và the same data type.
3. The file type is not: The file type is not a file type regardless of the type of data written to lớn the tệp tin. Data written to lớn the tệp tin without conversion.
The greathử nghiệm effect of the type of data tệp tin that you can store the data entered from the keyboard and the results processed in RAM khổng lồ use the tệp tin multiple times.
– Defining the file type in the từ khoá FILE OF follows from the description of the type TYPE, The next variable is declared in the variable declaration tệp tin.

TypeArr = array<1..100> of integer; dinc nghia mang ArrTArr = FILE of Arr; dinc nghia tep TArr teo cac phan tu la với ArrTStr = FILE of String<50>; tep TStr co cac phan tu la chuoi teo vì dai 50 ky tuSinhVien = RecordMsv, Hoten : String<50>;Diem : real;end;TSv = FILE of SinhVien; tep TSv gom cac phan tu la kieu SinhVienVarT1 : TArr;T2 : TStr;T3 : TSv;Variable definition files directly in the variable declarationVarT4 : FILE of array<1..100> of real;T5 : FILE of SinhVien;1. Enter exported khổng lồ a text fileAccess to lớn the tệp tin is interpreted as entering data into the tệp tin, recorded data on external storage devices, read the data on the screen or printer và handle it.

Before manipulating the files we need lớn use the procedures assign(bientep, tentep). This procedure aims to lớn assign a disk file (tentep) The variable name for the file in RAM (bientep).

mở cửa a new file khổng lồ record:

Assign(bientep, tentep);Rewrite(bientep);Procedures Rewrite create a disk tệp tin name assigned to lớn the variable name in order to lớn assign Assign File and open it lớn retrieve sầu data lieu.Khi open the file using the commvà Rewrite if the tệp tin on disk has coincided with the name you . Be careful when you open the tệp tin using the command Rewrite.

mở cửa the tệp tin has to put:

Assign(bientep, tentep);Append(bientep);xuất hiện the tệp tin lớn read data:Assign(bientep, tentep);Reset(bientep);Attention: When you open a file with a reset if no files on the disk will cause an error.

Final, we must cthất bại the tệp tin using the procedure:

This procedure passes the contents of memory to disk file và cthua trận the tệp tin release memory for processing files. The files when opened, if not cthua kém khổng lồ retrieve lost data on variables File Name.

Xem thêm: 6 Cách Kiểm Tra Số Dư Tài Khoản Ngân Hàng Agribank

The entry of data on variables of type File is only done as follows:– Read data from tệp tin using procedures Readln(bientep,well);– Write data khổng lồ disk: Using procedures Writeln(bientep,well);Example:

uses crt;var t : Text; str : string;BEGIN clrscr; assign(t, "inputText.txt"); gan ten bien tep mang đến tep rewrite(t); mo tep de ghi du lieu writeln(t, "nguyenvanquan"); writeln(t, "7826"); reset(t); mo tep de doc du lieu writeln("Data of tệp tin inputText.txt:"); trong những khi bé tro tep chua den cuoi tep thi cu doc while not eof(t) do begin readln(t, str); writeln(str); end; append(t); mo tep de ghi them du lieu writeln(t, "test"); close(t); reset(t); writeln; writeln("Data of file inputText.txt after change:"); trong những khi bé tro tep chua den cuoi tep thi cu doc while not eof(t) bởi begin readln(t, str); writeln(str); end; close(t); dong tep readln;END.