[Pascal – TUT] Posts 10: Files in pascal – File Type
1 The concept of file:
[qads]
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.
CLOSE(bientep);
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.
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.
Article referenced in:
codepascal.blogspot.com
z15.invisionfree.com
Oh let me ask Anh Quan:How to remove a portion of the file data?
I actually have not thought out and not try to do, but you can do by reading file, then record what's required file.
Thank you sir labor!
Thank you for the blog post
But I'm graders 8 and take the exam preparation solutions Informatics specialist so he can not tell me what to do about file content and easy to understand and simple is not it
All you know whip bared.
Inp vs different file somewhere out so he
inp the input file (input)
out the ouput file (output)
Data in a file is deleted when the program exits not that he
Not deleted jive.
e asked file type not declared how so?
I had instructions above got you.
I want to export in order from 1 to 3 , each contestant is 1 row;
My code is :……
reset(tSV);
for i=1 to i =3 do
start
write(sv.Msv,’ ');
write(sv.Hoten,’ ');
writeln(sv.Diem);
than;
close(tSV);
readln;
but does it only output last contestant à ?
Fearing no record on the file command, they must.
program Taptin;
uses crt;
type
thisinh = record
threats:string[10];
Sinh:integer;
Diemthi: real;
Output:(trungbinh,kha,cream,gioi);
than;
the MTS : file of thisinh;
in:integer;
sv:thisinh;
start
clrscr;
assign(MTS,’Thisinh.dat’);
rewrite(MTS);
for i:=1 to 2 do
start
write('Enter a name born collector',in,':');
readln(sv.Hoten);
write('Import boys:');
readln(sv.Namsinh);
write('Contest entry point:');
readln(sv.Diemthi);
write('Enter the result:');
readln(sv.Ketqua);
write(MTS,sv);
than;
close(MTS);
reset(MTS);
writeln;
writeln;
for i:=1 to 2 do
start
write(sv.Hoten,’ ');
write(sv.Namsinh,’ ');
write(sv.Diemthi:3:2,’ ');
writeln(sv.Ketqua);
than;
close(MTS);
readln;
than.
Code e Here ' . e with a look it
Why remove cmt sir ? e only questions about how to export files in the order ??
I put up the basic code, cha say, a processor also do not know how. Less view it must also describe how, do something about, mistakes out alright.
let me ask why persons
seek(TSv, 2);
read(TSv, sv);
Kids machine if to 0 it finds record 1; If 1 it finds record 2. Ie it increases 1 unlike in his post to find 2 it out 2.
Room for Kids always ask. How to take part Code formatted so part of his examples sir?
I want to refer to 1 A Web page.
Web users do their wordpress, it plugin then.
You see this article: https://cachhoc.net/2014/06/18/wordpess-hien-thi-code-tren-wordpress-voi-syntax-highlighted-posting-source-code-on-wordpress-with-syntax-highlighted/
Web users do their wordpress, it plugin then.
You see this article: https://cachhoc.net/2014/06/18/wordpess-hien-thi-code-tren-wordpress-voi-syntax-highlighted-posting-source-code-on-wordpress-with-syntax-highlighted/
A dear, e do forever without a result đk sir:
Program Khoang_Cach;
Where d:real;
f: text;
x,and: integer;
Begin
Assign(f,’ TRAI.TXT ');
Reset(f);
While not eof(f) do
Begin
Read(f,x,and);
d:= sqrt(x * x + y * y);
Writeln(‘ distance:', d:10:2)
End;
Close(f)
Readln;
End.
Forward looking e vs ad help fix bugs and help e
This should be viewed in the data file that you have not. How try debug view it?
Close(f) thiếu dấu ;
muốn mở tiệp ra và ghi giá trị vào cuối cùng của biến có kiểu thì sao? Không sử dụng đc Append?
thế làm sao để đọc dữ liệu văn bản trong tệp
ở trên anh mới hướng dẫn đọc dữ liệu số thôi
Có đọc text ở trên rồi mà 🙂
Em phải xem tệp TRAI.TXT có nhập dữ liệu vào chưa? Đúng quy cách không?
e ask: BTVN của e như sau: tính 1/1+2 +1/2+3 + 1/3+4 +…+1/(A-1)+n, e viết chương trình như sau:
program tinh;
uses crt;
var i,n: integer;
S: real;
start
clrscr;
write(‘Nhap so n: '); Readln(n);
S:=0;
fori:=1 to n do if i mod 2 0 then S:= S+1/((i-1)+in);
writeln(‘ Tong la: ',S+2018 :5:0);
readln
than.
e to write a program like this, but when does it run enter the result is 2019 over
see help e considered wrong somewhere e v sir?
help e with
why fixed 2018 output?