[Pascal – TUT] Posts 10: Files in pascal – File Type

1 The concept of file:

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.
File aggregation in it some element of data has the same structure but different array like array element number of the file is not defined.

[qads]

In Pascal 3 file type used is:
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 a text file to 4 Byte (not 2 Byte).
2. File type: The file is a file type that its elements have the same length and 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 the file. Data written to the file without conversion.
The greatest effect of the type of data file that you can store the data entered from the keyboard and the results processed in RAM to use the file multiple times.
* Declare:
– Defining the file type in the keyword FILE OF follows from the description of the type TYPE, The next variable is declared in the variable declaration file.
Example:
Type
	Arr = array[1..100] of integer; {dinh nghia mang Arr}
	TArr = FILE of Arr; {dinh nghia tep TArr co cac phan tu la mang Arr}
	TStr = FILE of String[50]; {tep TStr co cac phan tu la chuoi co do dai 50 ky tu}
	SinhVien = Record
		Msv, Hoten : String[50];
		Diem : real;
	end;
	TSv = FILE of SinhVien; {tep TSv gom cac phan tu la kieu SinhVien}
Var
	T1 : TArr;
	T2 : TStr;
	T3 : TSv;
  • Variable definition files directly in the variable declaration
Var
	T4 : FILE of array[1..100] of real;
	T5 : FILE of SinhVien;

1. Enter exported to a text file
Access to the file is interpreted as entering data into the file, recorded data on external storage devices, read the data on the screen or printer and handle it.

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

  • Open a new file to record:
Assign(bientep, tentep);
Rewrite(bientep);

Procedures Rewrite create a disk file name assigned to the variable name in order to assign Assign File and open it to retrieve data lieu.Khi open the file using the command Rewrite if the file on disk has coincided with the name you . Be careful when you open the file using the command Rewrite.

  • Open the file has to put:
Assign(bientep, tentep);
Append(bientep);
  • Open the file to 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 close the file using the procedure:
CLOSE(bientep);
This procedure passes the contents of memory to disk file and close the file release memory for processing files. The files when opened, if not close to retrieve lost data on variables File Name.

The entry of data on variables of type File is only done as follows:
– Read data from file using procedures Readln(bientep,well);
– Write data to disk: Using procedures Writeln(bientep,well);
Example:

uses crt;
var
        t : Text;
        str : string;
BEGIN
        clrscr;
        assign(t, 'inputText.txt'); {gan ten bien tep cho 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 file inputText.txt:');
        {trong khi con 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 khi con tro tep chua den cuoi tep thi cu doc}
        while not eof(t) do
        begin
                readln(t, str);
                writeln(str);
        end;

        close(t);   {dong tep}
        readln;
END.

file in pascal

2. File type
As we all know the file type text entry and cause trouble to record data such as read, recorded data 1 Students must make very complex. We can handle more simple file type.
Read and write :
– Write to File: Write(bientep,property.1, bien2 ...); with bien1, bien2, ... are variables of the same type with variable file.
– Read file: Read(bientep,property.1, bien2 ...);
Attention:
Unlike text file, write and read the file type does not use the writeln command or file type readln means not write data into the stream. The elements of the file type is recorded continuously in the memory cell and only end symbol EOF file. When we finished reading or writing a file element, the cursor will automatically move to the next position.

Access to the ith element of the file: Seek(bientep,in); i=0,1,2,…
Procedures seek to position the cursor at the ith position of the file.
The file handler:
* Filesize(bientep) indicates the number of elements in the file
* FilePos(bientep) said the current position of the file pointer
* Eof(Bientep) the value is True if the file pointer at the end of the file location, False backwards

Example:

uses crt;
Type
        SinhVien = Record
                MSv, Hoten : String;
                Diem : real;
        end;
Var
        TSv : FILE of SinhVien;
        sv : SinhVien;
        i : integer;
BEGIN
        clrscr;
        assign(TSv, 'SV.dat');
        rewrite(TSv);
        for i:= 1 to 3 do {ghi du lieu 3 sinh vien vao tep}
        begin
                {nhap du lieu}
                write('Nhap ma sinh vien thu ', i, ' : ');
                readln(sv.MSv);
                write('   Nhap ten sinh vien: ');
                readln(sv.Hoten);
                write('   Nhap diem cua sinh vien: ');
                readln(sv.diem);

                {ghi du lieu vao tep}
                write(TSv, sv);
        end;
        close(TSv);
        reset(TSv); {mo tep de doc}
        writeln;
        writeln('Thong tin sinh vien thu 2 trong tep:');
        seek(TSv, 2);
        read(TSv, sv);
        writeln('MSV: ', sv.MSv);
        writeln('Ho ten: ', sv.Hoten);
        writeln('Diem: ', sv.diem:3:2);
        close(TSv);
        readln;
END.

When you open a file, it will not be SV.dat find the desired information as it is written in binary form rather than its text but rather information is still accurate.
Import binary output in pascal

Article referenced in:
codepascal.blogspot.com
z15.invisionfree.com