//
you're reading...
Chủ đề file, Dùng cho cấp THCS, Dùng cho cấp THPT

Bài 13: Kiểu dữ liệu tập tin (file)

Pascal – File Handling

http://www.tutorialspoint.com/pascal/pascal_files_handling.htm

Copyright © tutorialspoint.com

Pascal treats a file as a sequence of components, which must be of uniform type. A file’s type is determined by the type of the components. File data type is defined as:

type
file-name = file of base-type;

Where, the base-type indicates the type of the components of the file. The base type could be anything like, integer, real, Boolean, enumerated, subrange, record, arrays and sets except another file type. Variables of a file type are created using the var declaration:

var
f1, f2,...: file-name;

Following are some examples of defining some file types and file variables:

type
   rfile = file of real;
   ifile = file of integer;
   bfile = file of boolean;
   datafile = file of record
   arrfile = file of array[1..4] of integer;
var
   marks: arrfile;
   studentdata: datafile;
   rainfalldata: rfile;
   tempdata: ifile;
   choices: bfile;

Creating and Writing to a File

Let us write a program that would create a data file for students’ records. It would create a file named students.dat and write a student’s data into it:

program DataFiles;
type
   StudentRecord = Record
      s_name: String;
      s_addr: String;
      s_batchcode: String;
   end;
var
   Student: StudentRecord;
   f: file of StudentRecord;

begin
   Assign(f,'students.dat');
   Rewrite(f);
   Student.s_name := 'John Smith';
   Student.s_addr := 'United States of America';
   Student.s_batchcode := 'Computer Science';
   Write(f,Student);
   Close(f);
end.

When compiled and run, the program would create a file named students.dat into the working directory. You can open the file using a text editor, like notepad, to look at John Smith’s data.

Reading from a File

We have just created and written into a file named students.dat. Now, let us write a program that would read the student’s data from the file:

program DataFiles;
type
   StudentRecord = Record
      s_name: String;
      s_addr: String;
      s_batchcode: String;
   end;
var
   Student: StudentRecord;
   f: file of StudentRecord;
begin
   assign(f, 'students.dat');
   reset(f); 
   while not eof(f) do
   begin
      read(f,Student);
      writeln('Name: ',Student.s_name);
      writeln('Address: ',Student.s_addr);
      writeln('Batch Code: ', Student.s_batchcode);
   end;
   close(f);
end.

When the above code is compiled and executed, it produces the following result:

Name: John Smith
Address: United States of America
Batch Code: Computer Science

Files as Subprogram Parameter

Pascal allows file variables to be used as parameters in standard and user-defined subprograms. The following example illustrates this concept. The program creates a file named rainfall.txt and stores some rainfall data. Next, it opens the file, reads the data and computes the average rainfall.

Please note that, if you use a file parameter with subprograms, it must be declared as a var parameter.

program addFiledata;
const
   MAX = 4;
type
   raindata = file of real;
var
   rainfile: raindata;
   filename: string;
procedure writedata(var f: raindata);
var
   data: real;
   i: integer;
begin
   rewrite(f, sizeof(data));
   for i:=1 to MAX do
   begin
      writeln('Enter rainfall data: ');
      readln(data);
      write(f, data);
   end;
   close(f);
end;

procedure computeAverage(var x: raindata);
var
   d, sum: real;
   average: real;
begin
   reset(x);
   sum:= 0.0;
   while not eof(x) do
   begin
      read(x, d);
      sum := sum + d;
   end;
   average := sum/MAX;
   close(x);
   writeln('Average Rainfall: ', average:7:2);
end;
begin
   writeln('Enter the File Name: ');
   readln(filename);
   assign(rainfile, filename);
   writedata(rainfile);
   computeAverage(rainfile);
end.

When the above code is compiled and executed, it produces the following result:

Enter the File Name:
rainfall.txt
Enter rainfall data:
34
Enter rainfall data:
45
Enter rainfall data:
56
Enter rainfall data:
78
Average Rainfall: 53.25

Text Files

A text file, in Pascal, consists of lines of characters where each line is terminated with an end-of-line marker. You can declare and define such files as:

type
file-name = text;

Difference between a normal file of characters and a text file is that a text file is divided into lines, each terminated by a special end-of-line marker, automatically inserted by the system. The following example creates and writes into a text file named contact.txt:

program exText;
var
   filename, data: string;
   myfile: text;
begin
   writeln('Enter the file name: ');
   readln(filename);
   assign(myfile, filename);
   rewrite(myfile);
   writeln(myfile, 'Note to Students: ');
   writeln(myfile, 'For details information on Pascal Programming');
   writeln(myfile, 'Contact: Tutorials Point');
   writeln('Completed writing'); 
   close(myfile);
end.

When the above code is compiled and executed, it produces the following result:

Enter the file name:
contact.txt 
Completed writing

Appending to a File

Appending to a file means writing to an existing file that already has some data without overwriting the file. The following program illustrates this:

program exAppendfile;
var
   myfile: text;
   info: string;
begin
   assign(myfile, 'contact.txt');
   append(myfile);
   writeln('Contact Details');
   writeln('webmaster@tutorialspoint.com');
   close(myfile);
   (* let us read from this file *)
   assign(myfile, 'contact.txt');
   reset(myfile);
   while not eof(myfile) do
   begin
      readln(myfile, info);
      writeln(info);
   end;
   close(myfile);
end.

When the above code is compiled and executed, it produces the following result:

Contact Details
webmaster@tutorialspoint.com
Note to Students:
For details information on Pascal Programming
Contact: Tutorials Point

File Handling Functions

Free Pascal provides the following functions/procedures for file handling:

S.N Function Name & Description
1 procedure Appendvart:Text;
Opens a file in append mode
2 procedure Assignoutf:file;constName:;
Assigns a name to a file
3 procedure Assignoutf:file;p:PChar;
Assigns a name to a file
4 procedure Assignoutf:file;c:Char;
Assigns a name to a file
5 procedure Assignoutf:TypedFile;constName:;
Assigns a name to a file
6 procedure Assignoutf:TypedFile;p:PChar;
Assigns a name to a file
7 procedure Assignoutf:TypedFile;c:Char;
Assigns a name to a file
8 procedure Assignoutt:Text;consts:;
Assigns a name to a file
9 procedure Assignoutt:Text;p:PChar;
Assigns a name to a file
10 procedure Assignoutt:Text;c:Char;
Assigns a name to a file
11 procedure BlockReadvarf:file;varBuf;count:Int64;varResult:Int64;
Reads data from a file into memory
12 procedure BlockReadvarf:file;varBuf;count:LongInt;varResult:LongInt;
Reads data from a file into memory
13 procedure BlockReadvarf:file;varBuf;count:Cardinal;varResult:Cardinal;
Reads data from a file into memory
14 procedure BlockReadvarf:file;varBuf;count:Word;varResult:Word;
Reads data from a file into memory
15 procedure BlockReadvarf:file;varBuf;count:Word;varResult:Integer;
Reads data from a file into memory
16 procedure BlockReadvarf:file;varBuf;count:Int64;
Reads data from a file into memory
17 procedure BlockWritevarf:file;constBuf;Count:Int64;varResult:Int64;
Writes data from memory to a file
18 procedure BlockWritevarf:file;constBuf;Count:LongInt;varResult:LongInt;
Writes data from memory to a file
19 procedure BlockWritevarf:file;constBuf;Count:Cardinal;varResult:Cardinal;
Writes data from memory to a file
20 procedure BlockWritevarf:file;constBuf;Count:Word;varResult:Word;
Writes data from memory to a file
21 procedure BlockWritevarf:file;constBuf;Count:Word;varResult:Integer;
Writes data from memory to a file
22 procedure BlockWritevarf:file;constBuf;Count:LongInt;
Writes data from memory to a file
23 procedure Closevarf:file;
Closes a file
24 procedure Closevart:Text;
Closes a file
25 function EOFvarf:file:Boolean;
Checks for end of file
26 function EOFvart:Text:Boolean;
Checks for end of file
27 function EOF: Boolean;
Checks for end of file
28 function EOLnvart:Text:Boolean;
Checks for end of line
29 function EOLn: Boolean;
Checks for end of line
30 procedure Erasevarf:file;
Deletes file from disk
31 procedure Erasevart:Text;
Deletes file from disk
32 function FilePosvarf:file:Int64;
Position in file
33 function FileSizevarf:file:Int64;
Size of file
34 procedure Flushvart:Text;
Writes file buffers to disk
35 function IOResult: Word;
Returns result of last file IO operation
36 procedure ReadvarF:Text;Args:Arguments;
Reads from file into variable
37 procedure ReadArgs:Arguments;
Reads from file into variable
38 procedure ReadLnvarF:Text;Args:Arguments;
Reads from file into variable and goto next line
39 procedure ReadLnArgs:Arguments;
Reads from file into variable and goto next line
40 procedure Renamevarf:file;consts:;
Renames file on disk
41 procedure Renamevarf:file;p:PChar;
Renames file on disk
42 procedure Renamevarf:file;c:Char;
Renames file on disk
43 procedure Renamevart:Text;consts:;
Rename file on disk
44 procedure Renamevart:Text;p:PChar;
Renames file on disk
45 procedure Renamevart:Text;c:Char;
Renames file on disk
46 procedure Resetvarf:file;l:LongInt;
Opens file for reading
47 procedure Resetvarf:file;
Opens file for reading
48 procedure Resetvarf:TypedFile;
Opens file for reading
49 procedure Resetvart:Text;
Opens file for reading
50 procedure Rewritevarf:file;l:LongInt;
Opens file for writing
51 procedure Rewritevarf:file;
Opens file for writing
52 procedure Rewritevarf:TypedFile;
Opens file for writing
53 procedure Rewritevart:Text;
Opens file for writing
54 procedure Seekvarf:file;Pos:Int64;
Sets file position
55 function SeekEOFvart:Text:Boolean;
Sets file position to end of file
56 function SeekEOF: Boolean;
Sets file position to end of file
57 function SeekEOLnvart:Text:Boolean;
Sets file position to end of line
58 function SeekEOLn: Boolean;
Sets file position to end of line
59 procedure SetTextBufvarf:Text;varBuf;
Sets size of file buffer
60 procedure SetTextBufvarf:Text;varBuf;Size:SizeInt;
Sets size of file buffer
61 procedure TruncatevarF:file;
Truncate the file at position
62 procedure WriteArgs:Arguments;
Writes variable to file
63 procedure WritevarF:Text;Args:Arguments;
Write variable to file
64 procedure WritelnArgs:Arguments;
Writes variable to file and append newline
65 procedure WriteLnvarF:Text;Args:Arguments;
Writes variable to file and append newline

Bài tập

.

Bài tập

 Program Tap_Tin_So_Nguyen;
Uses Crt;
Var
 f : File Of Integer;
 So : Integer;
 a : Array[1..10] Of Integer;
 Spt,i,j : Integer;
 Filename : String[11];
Begin
 ClrScr;
 Writeln('NHAP SO LIEU CHO TAP TIN SONGUYEN.DAT');
 Writeln('-------------------------------------');
 Writeln;
 Assign(f,'songuyen.dat');
 ReWrite(f);
 For i := 1 To 10 Do
 Begin
 Write('-So thu: ',i:2,' = ');
 Readln(So);
 Write(f,so);
 End;
 Close(f);
 Spt := 0;
 Reset(f);
 While NOT EOF(f) Do
 Begin
 Spt := Spt + 1;
 Read(f,so);
 a[Spt] := so;
 End;
 Close(f);
 For i := 1 To Spt -1 Do
 For j := Spt Downto i + 1 Do
 If a[j] < a[j-1] Then
 Begin
 So := a[j];
 a[j] := a[j-1];
 a[j-1] := So;
 End;
 Writeln;
 Writeln('Sau khi sap xep: ');
 For i := 1 To 10 Do
 Write(a[i]:6);
 Writeln;
 Write(' Bam <Enter>... ');
 Readln
End.

.

Bài tập

 Program Ghep_Tap_Tin;
Uses Crt;
Var
 f1,f2,f3 : File Of Real;
 i : Integer;
 So : Real;
 Ch : Char;
Begin
 ClrScr;
 Writeln('NHAP SO LIEU CHO TAP1.DAT');
 Writeln('-------------------------');
 Writeln;
 Assign(f1,'TAP1.DAT');
 Rewrite(f1);
 i := 0;
 Repeat
 i := i + 1;
 Write('-So thu: ',i:2,' = ');
 Readln(So);
 Write(f1,so);
 Write(' Nhap nua ? (c/k) ');
 Readln(Ch);
 Until Upcase(Ch) ='K';
 Close(f1);
 ClrScr;
 Writeln('NHAP SO LIEU CHO TAP2.DAT');
 Writeln('-------------------------');
 Writeln;
 Assign(f2,'TAP2.DAT');
 Rewrite(f2);
 i := 0;
 Repeat
 i := i + 1;
 Write('-So thu: ',i:2,' = ');
 Readln(So);
 Write(f2,so);
 Write(' Nhap nua ? (c/k) ');
 Readln(Ch);
 Until Upcase(Ch) ='K';
 Close(f2);
 ClrScr;
 Writeln('GHEP TAP1 va TAP2 thanh TAP3');
 Writeln('----------------------------');
 Writeln;
 Assign(f3,'TAP3.DAT');
 Rewrite(f3);
 Reset(f1);
 Reset(f2);
 While NOT EOF(f1) Do
 Begin
 Read(f1,So);
 Write(f3,So);
 End;
 While NOT EOF(f2) Do
 Begin
 Read(f2,so);
 Write(f3,so);
 End;
 Reset(f3);
 While NOT EOF(f3) Do
 Begin
 Read(f3,So);
 Write(So :8:1);
 End;
 Close(f1);
 Close(f2);
 Close(f3);
 Writeln;
 Writeln;
 Write(' Da ghep xong, Bam <Enter>...');
 Readln;
End.

.

Bài tập

 Program So_tay_Dien_Tu;
Uses Crt;
Type
 DienThoai = RECORD
 HoTen : String[24];
 Tel : LongInt;
 Add : String;
 End;
Var
 f : File Of DienThoai;
 Tam : DienThoai;
 St : String;
 TimThay : Boolean;
Begin
 ClrScr;
 Writeln('NHAP SO DIEN THOAI VA DIA CHI');
 Writeln('-----------------------------');
 Writeln;
 Assign(f,'Telephon.dat');
 Rewrite(f);
 With tam Do
 Repeat
 Write('-Ho ten, bam (0> de ket thuc: ');
 Readln(HoTen);
 If HoTen <> '0' Then
 Begin
 Write('-So phone : ');
 Readln(Tel);
 Write('-Dia chi : ');
 Readln(Add);
 Write(f,tam);
 End;
 Until Hoten = '0';
 Close(f);
 ClrScr;
 Writeln('TIM SO DIEN THOAI VA DIA CHI');
 Writeln('----------------------------');
 Writeln;
 Write('-Ho ten nguoi muon tim: ');
 Readln(St);
 Reset(f);
 TimThay := False;
 While NOT EOF(f) Do
 Begin
 Read(f,Tam);
 With Tam Do
 If St = HoTen then
 Begin
 TimThay := True;
 Writeln(HoTen);
 Writeln('-So Telephone: ',Tel);
 Writeln('-Dia chi : ',Add);
 End;
 End;
 If Not TimThay Then
 Writeln('Khong tim thay');
 Close(f);
 Writeln;
 Write(' Bam <Enter>... ');
 Readln
End.

.

Bài tập

 Program Nhap_Du_Lieu;
Uses Crt;
Type
 HoSo = RECORD
 Holot : String[17];
 Ten : String[7];
 Tuoi : 18..60;
 ChucVu: String[20];
 BacLuong : 300000..900000;
 End;
Var
 f : File Of HoSo;
 Nv : HoSo;
 ans : Char;
Begin
 ClrScr;
 Writeln('NHAP DU LIEU VAO TAP TIN LUONG.DAT');
 Writeln('----------------------------------');
 Writeln;
 Assign(f,'LUONG.DAT');
 Rewrite(f);
 Repeat
 With Nv Do
 Begin
 Write('-Ho lot: ');
 Readln(Holot);
 Write('-Ten : ');
 Readln(Ten);
 Write('-Tuoi : ');
 Readln(tuoi);
 Write('-Chuc vu : ');
 Readln(ChucVu);
 Write('-Bac luong: ');
 Readln(BacLuong);
 Write(f,Nv);
 End;
 ans := Readkey;
 Until ans = #27;
 Close(f);
End.

.

Bài tập

 Program Cap_Nhat_Du_Lieu;
Uses Crt;
Type
 HoSo = RECORD
 Holot : String[17];
 Ten : String[7];
 Tuoi : 18..60;
 ChucVu: String[20];
 BacLuong : 300000..900000;
 End;
Var
 f : File Of HoSo;
 Nv : HoSo;
 ans : Char;
Begin
 ClrScr;
 Writeln('CAP NHAT DU LIEU VAO TAP TIN LUONG.DAT');
 Writeln('----------------------------------');
 Writeln;
 Assign(f,'LUONG.DAT');
 Reset(f);
 Seek(f,Filesize(f));
 Repeat
 With Nv Do
 Begin
 Write('-Ho lot: ');
 Readln(Holot);
 Write('-Ten : ');
 Readln(Ten);
 Write('-Tuoi : ');
 {$R+}
 Readln(tuoi);
 Write('-Chuc vu : ');
 Readln(ChucVu);
 Write('-Bac luong: ');
 Readln(BacLuong);
 Write(f,Nv);
 End;
 ans := Readkey;
 Until ans = #27;
 Close(f);
End.

.

Bài tập

 Program CheDau_TapTin;
Uses Dos,Crt;
Var
 f : File; {hoac f : Text }
 Filename : String;
 Ch : Char;
Begin
 Repeat
 ClrScr;
 TextColor(14);
 TextBackGround(2);
 GotoXY(23,4);
 Writeln('DAT THUOC TINH CHE DAU TAP TIN');
 GotoXY(23,5);
 Writeln('------------------------------');
 Writeln;
 TextColor(12);
 GotoXY(11,6);
 Writeln('*Khong hien thi duoc ten tap tin khi dung lenh DIR cua DOS*');
 GotoXY(15,8);
 TextColor(1);
 TextBackGround(14);
 Write('-Cho biet ten tap tin: ');
 Readln(Filename);
 TextColor(4+Blink);
 TextBackGround(14);
 GotoXY(25,24);
 Writeln('DANG THUC HIEN, XIN CHO DOI...');
 Assign(f,Filename);
 SetFAttr(f,Hidden);
 TextColor(4);
 TextBackGround(15);
 GotoXY(15,10);
 Case DosError Of
 0 : Writeln('Da hoan thanh tot dep');
 2 : Writeln('Khong tim thay tap tin nay');
 3 : Writeln('Khong tim thay duong dan')
 Else
 Writeln('Tap tin duoc bao ve, khong sua duoc');
 End;
 GotoXY(19,24);
 TextColor(14);
 TextBackGround(4);
 Writeln('Bam phim bat ky de tiep tuc, <Esc> de thoat ');
 Ch := Readkey;
 TextColor(White);
 TextBackGround(Black);
 ClrScr;
 Until Ch = #27;
End.

.

Bài tập

 Program Doc_Tap_tin;
Var
 f : Text;
 Filename : String;
 Ch : Char;
Begin
 Writeln('DOC TAP TIN VAN BAN');
 Writeln('-------------------');
 Writeln;
 Write('-Cho biet ten tap tin: ');
 Readln(Filename);
 Assign(f,filename);
 Reset(f);
 While Not EOF(f) Do
 Begin
 Read(f,Ch);
 Write(Ch);
 End;
 Close(f);
 Writeln;
 Write('Bam <Enter>... ');
 Readln
End.

.

Bài tập

 Program Doc_Tung_Chu;
Uses Crt;
Var
 Filename : String;
 f : Text;
 Line : String[251];
 k : Integer;
Begin
 ClrScr;
 Writeln('DOC CHAM TAP TIN THEO TUNG CHU');
 Writeln('------------------------------');
 Writeln;
 Write('-Cho biet ten tap tin: ');
 Readln(Filename);
 Assign(f,Filename);
 Reset(f);
 While Not EOF(f) Do
 Begin
 Readln(f,line);
 For k := 1 To Length(line) Do
 Begin
 Write(line[k]);
 Delay(150);
 End;
 Writeln;
 End;
 Close(f);
End.

.

Bài tập

 Program Doc_Ghi_Tap_Tin;
Var
 f1,f2 : Text;
 Filename : String;
 i : Integer;
 St : String;
Begin
 Writeln('DANH SO DONG TAP TIN');
 Writeln('--------------------');
 Writeln;
 Write('-Cho biet ten tap tin: ');
 Readln(Filename);
 Assign(f1,filename);
 Reset(f1);
 Assign(f2,'Newfile.txt');
 Rewrite(f2);
 i := 0;
 While Not EOF(f1) Do
 Begin
 i := i + 1;
 Readln(f1,st);
 Writeln(f2,i,' ',St);
 End;
 Close(f1);
 Close(f2);
 Writeln;
 Writeln('Da danh so dong va ghi vao tap tin NEWFILE.TXT');
 Writeln;
 Write(' Bam <Enter> de xem tap tin NEWFILE.TXT ');
 Readln;
 Assign(f2,'Newfile.txt');
 Reset(f2);
 While Not EOF(f2) Do
 Begin
 Readln(f2,st);
 Writeln(St);
 End;
 Close(f2);
 Writeln;
 Write('Xem xong, ban <Enter> ');
 Readln
End.

.

Bài tập

 Program Lambada;
Uses Crt;
Var
 f : Text;
 Note,dur : Word;
 Buf : Array[1..10240] Of Byte;
{---------------------------------}
 Procedure Play(Caodo,Truongdo : Word);
 Begin
 Sound(Caodo);
 Delay(Truongdo);
 NoSound;
 End;
{---------------------------------}
BEGIN
 ClrScr;
 TextColor(Yellow);
 Writeln('TAO TAP TIN AM THANH');
 TextColor(Red);
 Writeln('--------------------');
 Writeln;
 TextColor(Green);
 Writeln('Bam phim bat ky de tat am thanh');
 Repeat
 Assign(f,'lambada.not');
 SetTextBuf(f,buf);
 Reset(f);
 While (Not EOF(f)) And (Not KeyPressed) Do
 Begin
 Readln(f,Note,dur);
 Play(Note,Dur);
 End;
 Until KeyPressed;
 Close(f);
 NoSound;
END.

.

Bài tập

 Program Karaoke;
Uses Crt;
Const
 Lento = 10;
Type
 ConTro = ^Nhac;
 Nhac = RECORD
 Note,Dura : Word;
 Next : ConTro;
 End;
Var
 f : Text;
 P,First, Last,HeapTop : ConTro;
{------------------------------------}
 Procedure AssignList(Filename : String);
 Begin
 Assign(f,Filename);
 {$I-}
 Reset(f);
 {$I+}
 If IOResult <> 0 Then
 Halt(1);
 First := Nil;
 Mark(HeapTop);
 While NOt EOF(f) Do
 Begin
 New(p);
 Readln(f,p^.Note,p^.Dura);
 If First = Nil Then
 First := p
 Else
 Last^.Next := p;
 Last := p;
 Last^.Next := Nil;
 End;
 Close(f);
 End;
{------------------------------------}
 Procedure Music;
 Begin
 p := First;
 While (p <> Nil) And Not (KeyPressed And (Readkey = #27)) Do
 Begin
 Sound(p^.Note);
 Delay(Lento*p^.Dura);
 p := p^.Next;
 If p = Nil Then
 p := First;
 End;
 End;
{------------------------------------}
BEGIN
 ClrScr;
 Writeln(' CHUONG TRINH NHAC EM DIU');
 Writeln('Truong do cham 10 lan so voi BT15_10');
 Writeln(' Bam <Esc> de ket thuc');
 Writeln('-------------------------------------');
 AssignList('Lambada.Not');
 Music;
 NoSound;
END.

.

Bài tập

 Program Nhap_Diem;
Type
 HocBa = RECORD
 HoTen : String[24];
 Van,Toan,Ly,Hoa,Tb: Real;
 End;
 FileHB = File Of HocBa;
Var
 f : FileHB;
 HS : HocBa;
 q : Boolean;
Begin
 Writeln('CHUONG TRINH GHI DIEM VAO TAP TIN');
 Writeln('--------------------------------------');
 Writeln;
 Assign(f,'DIEM.DAT');
 ReWrite(f);
 q := True;
 While q Do
 With HS Do
 Begin
 Write('-Ho ten hoc sinh,(<Enter> de ket thuc): ');
 Readln(HoTen);
 If HoTen = '' Then
 Q := False
 Else
 Begin
 Write('-Diem Van : ');
 Readln(Van);
 Write('-Diem Toan: ');
 Readln(Toan);
 Write('-Diem Ly : ');
 Readln(Ly);
 Write('-Diem Hoa : ');
 Readln(Hoa);
 Tb := ((Van*2)+(Toan*2)+Ly+Hoa)/6;
 Write(f,HS);
 End;
 End;
End.

.

Bài tập

 Program Nhap_Them_Diem;
Type
 HocBa = RECORD
 HoTen : String[24];
 Van,Toan,Ly,Hoa,Tb: Real;
 End;
 FileHB = File Of HocBa;
Var
 f : FileHB;
 HS : HocBa;
 q : Boolean;
Begin
 Writeln('CHUONG TRINH GHI THEM DIEM VAO TAP TIN');
 Writeln('--------------------------------------');
 Writeln;
 Assign(f,'DIEM.DAT');
 Reset(f);
 Seek(f,filesize(f));
 q := True;
 While q Do
 With HS Do
 Begin
 Write('-Ho ten hoc sinh,(<Enter> de ket thuc): ');
 Readln(HoTen);
 If HoTen = '' Then
 Q := False
 Else
 Begin
 Write('-Diem Van : ');
 Readln(Van);
 Write('-Diem Toan: ');
 Readln(Toan);
 Write('-Diem Ly : ');
 Readln(Ly);
 Write('-Diem Hoa : ');
 Readln(Hoa);
 Tb := ((Van*2)+(Toan*2)+Ly+Hoa)/6;
 Write(f,HS);
 End;
 End;
End.

.

Bài tập

 Program Timp_Diem;
Type
 HocBa = RECORD
 HoTen : String[24];
 Van,Toan,Ly,Hoa,Tb: Real;
 End;
 FileHB = File Of HocBa;
Var
 f : FileHB;
 Hs : HocBa;
 St : String;
 TimThay : Boolean;
Begin
 Writeln('CHUONG TRINH TIM DIEM THEO HO TEN');
 Writeln('---------------------------------');
 Writeln;
 Write('-Ho ten hoc sinh muon tim: ');
 Readln(St);
 Assign(f,'DIEM.DAT');
 Reset(f);
 Timthay := False;
 While Not EOF(f) Do
 Begin
 Read(f,Hs);
 With Hs Do
 If St = HoTen Then
 Begin
 TimThay := True;
 Writeln(HoTen);
 Writeln('-Diem Van : ',Van:5:2);
 Writeln('-Diem Toan: ',Toan:5:2);
 Writeln('-Diem Ly : ',Ly:5:2);
 Writeln('-Diem Hoa : ',Hoa:5:2);
 Writeln('-Diem trung binh := ',Tb:5:2);
 End
 End;
 If Not Timthay Then
 Writeln('Trong danh sach khong co hoc sinh: ',St);
 Close(f);
 Writeln;
 Write(' Bam <Enter>... ');
 Readln;
End.

.

Bài tập

 Program Ho_So;
Uses Crt;
Type
 LyLich = RECORD
 HoTen : String[24];
 NamSinh : Integer;
 ChucVu : String[20];
 BacLuong : Real;
 End;
Var
 f : File Of LyLich;
 q : Boolean;
 Nv : LyLich;
 Stt : Integer;
 Ch : Char;
Begin
 ClrScr;
 Assign(f,'HOSO.DAT');
 Rewrite(f);
 q := True;
 While q Do
 With Nv Do
 Begin
 Write('-Ho ten CBCNV (<Enter> de ngung): ');
 Readln(HoTen);
 If HoTen = '' Then
 q := False
 Else
 Begin
 Write('-Nam sinh: ');
 Readln(NamSinh);
 Write('-Chuc vu: ');
 Readln(Chucvu);
 Write('-Bac luong: ');
 Readln(BacLuong);
 Write(f,Nv);
 End;
 End;
 Repeat
 GotoXY(28,24);
 Write('Co can sua khong? (c/k) ');
 Readln(Ch);
 If UpCase(Ch) = 'C' Then
 Begin
 Write('-Thay doi so thu tu : ');
 Readln(Stt);
 If Stt >= 1 Then
 Begin
 Seek(f,Stt-1);
 With Nv Do
 Begin
 Write('-Ho ten CBCNV : ');
 Readln(HoTen);
 Write('-Nam sinh: ');
 Readln(NamSinh);
 Write('-Chuc vu: ');
 Readln(Chucvu);
 Write('-Bac luong: ');
 Readln(BacLuong);
 Write(f,Nv);
 End;
 End;
 End;
 Until UpCase(Ch) = 'K';
 Close(f)
End.

.

Bài tập

 Program Tron_Tap_tin;
Uses Crt;
Var
 f1,f2,f3 : File Of Integer;
 So1,So2 : Integer;
 i : Integer;
 Ch : Char;
 ok1,ok2 : Boolean;
{--------------------------------}
 Function Layso1(Var So1 : Integer): Boolean;
 Begin
 If Not EOF(f1) Then
 Begin
 Read(f1,So1);
 LaySo1 := True;
 End
 Else
 layso1 := False;
 End;
{--------------------------------}
 Function Layso2(Var So2 : Integer): Boolean;
 Begin
 If Not EOF(f2) Then
 Begin
 Read(f2,So2);
 LaySo2 := True;
 End
 Else
 layso2 := False;
 End;
{--------------------------------}
BEGIN
 Assign(f1,'so1.dat');
 Rewrite(f1);
 ClrScr;
 Writeln('NHAP TAP TIN SO1');
 Writeln('----------------');
 Writeln;
 i := 0;
 Repeat
 i := i + 1;
 Write('-So thu: ',i:2,' = ');
 Readln(So1);
 Write(f1,so1);
 Write('-Nhap nua ? (c/k) ');
 Readln(Ch);
 Until Upcase(Ch) = 'K';
 Close(f1);
 Assign(f2,'so2.dat');
 Rewrite(f2);
 ClrScr;
 Writeln('NHAP TAP TIN SO2');
 Writeln('----------------');
 Writeln;
 i := 0;
 Repeat
 i := i + 1;
 Write('-So thu: ',i:2,' = ');
 Readln(So2);
 Write(f2,so2);
 Write('-Nhap nua ? (c/k) ');
 Readln(Ch);
 Until Upcase(Ch) = 'K';
 Close(f2);
 Assign(f3,'so3.dat');
 Rewrite(f3);
 Reset(f1);
 Reset(f2);
 ok1 := Layso1(So1);
 ok2 := Layso2(so2);
 While ok1 Or ok2 Do
 Begin
 If ok1 And ok2 Then {co ca 2 tap tin}
 Begin
 If So1 < So2 Then
 Begin
 Write(f3,so1);
 ok1 := layso1(so1);
 End
 Else
 Begin
 Write(f3,so2);
 ok2 := Layso2(so2);
 End;
 End
 Else
 If ok1 Then {chi con tap tin so1.dat}
 Begin
 Write(f3,so1);
 ok1 := layso1(so1);
 End
 Else
 If ok2 Then {chi con tap tin so2.dat}
 Begin
 Write(f3,so2);
 ok2 := Layso2(so2);
 End;
 End;
 Writeln;
 Reset(f3);
 While not EOF(f3) Do
 Begin
 Read(f3,i);
 Write(i:6);
 End;
 Close(f1);
 Close(f2);
 Close(f3);
 Writeln;
 Write('Da tron xong, bam <Enter>... ');
 Readln
END.

.

Bài tập

 Program Doi_Tuyen;
Uses Crt;
Type
 HocSinh = RECORD
 HoTen : String[24];
 Lop : String[4];
 Dtb : Real;
 End;
 Fhs = File Of HocSinh;
Var
 f : Fhs;
 Ch : Char;
{----------------------------------}
 Procedure Nhap(Var f : Fhs);
 Var
 Tam : HocSinh;
 Begin
 Rewrite(f);
 Repeat
 Write('-Nhap ho ten (0 de thoat): ');
 Readln(Tam.Hoten);
 If Tam.HoTen <> '0' Then
 Begin
 Write('-Lop: ');
 Readln(Tam.Lop);
 Write('-Diem trung binh: ');
 Readln(Tam.Dtb);
 Write(f,Tam);
 End;
 Until tam.HoTen ='0';
 Close(f);
 End;
{----------------------------------}
 Procedure Lapds(Var f:Fhs);
 Var
 Tam : Hocsinh;
 a : Array[1..1000] Of HocSinh;
 TenLop : Array[1..100] Of String[4];
 alop : Array[1..200] Of HocSinh;
 Sohs12,Solop12,Sohslop : Integer;
 Stt : Integer;
 i,j,k : Integer;
 Coroi : Boolean;
 Begin
 Reset(f);
 Sohs12 := 0;
 Solop12 := 0;
 While Not EOF(f) Do
 Begin
 Read(f,tam);
 If (Tam.Lop[1]='1') And (Tam.Lop[2]='2') Then
 Begin
 Sohs12 := Sohs12 + 1;
 a[Sohs12] := Tam;
 Coroi := False;
 For i := 1 To Solop12 Do
 If Tenlop[i] = Tam.Lop Then
 Coroi := True;
 If Not coroi Then
 Begin
 Solop12 :=Solop12+1;
 TenLop[Solop12] := Tam.Lop;
 End;
 End;
 End;
 Writeln('|','STT','|','HO VA TEN':24,'|',' LOP ','|',' HANG ','|');
 Stt := 1;
 For i := 1 to Solop12 Do
 Begin
 Sohslop := 0;
 For j := 1 To Sohs12 Do
 If a[j].Lop = Tenlop[i] Then
 Begin
 Sohslop := Sohslop + 1;
 alop[sohslop]:=a[j];
 End;
 For k := 1 To Sohslop - 1 Do
 For j := sohslop DownTo k + 1 Do
 If alop[j].Dtb > alop[j-1].Dtb Then
 Begin
 Tam := alop[j];
 alop[j] := alop[j-1];
 alop[j-1] := Tam;
 End;
 If Sohslop >=3 Then
 For k := 1 To 3 Do
 Begin
 With alop[k] Do
 Writeln('|',stt:3,' |',HoTen:24,' | ',
 Lop:5,' | ',k : 3,' |');
 Stt := Stt + 1;
 End
 Else
 For k := 1 To Sohslop Do
 Begin
 With alop[k] Do
 Writeln('|',stt:3,' |',HoTen:24,' | ',
 Lop:5,' | ',k : 3,' |');
 Stt := Stt + 1;
 End;
 End;
 Close(f);
 End;

{----------------------------------}
BEGIN
 Assign(f,'doituyen.dat');
 Repeat
 Repeat
 Writeln('1-Nhap du lieu');
 Writeln('2-Danh sach doi du tuyen');
 Writeln('3-Ket thuc');
 Ch := Readkey;
 Until ch in ['1'..'3'];
 Case Ch Of
 '1' : Nhap(f);
 '2' : Lapds(f);
 End;
 Until Ch = '3';
END.

.

Bài tập

 Program Tao_Tap_Tin_Co_Kieu;
Type
 HocSinh = RECORD
 Ten : String[7];
 Diem : 0..10;
 End;
Var
 f : File Of Hocsinh;
{-------------------------------}
 Procedure TaoTapTin;
 Var
 Tam : HocSinh;
 Filename : String;
 Begin
 Write('-Cho biet ten tap tin: ');
 Readln(Filename);
 Assign(f,Filename);
 {$I-}
 Rewrite(f);

 {$I+}
 If IOResult <> 0 Then
 Begin
 Writeln('Khong mo duoc tap tin: ',Filename);
 Halt;
 End;
 Repeat
 Write('Ten (bam <Enter> de cham dut) : ');
 Readln(Tam.Ten);
 If Tam.Ten <> '' Then
 Begin
 Write('-Diem : ');
 Readln(Tam.Diem);
 Write(f,Tam);
 End;
 Until Tam.Ten = '';
 Close(f);
 End;
{-------------------------------}
 Procedure XemLaiBanGhi;
 Var
 RecNo : Word;
 Tam : HocSinh;
 Begin
 Write('-Xem lai ban ghi thu may: ');
 Readln(RecNo);
 Reset(f);
 Seek(f,RecNo-1);
 Read(f,Tam);
 Writeln('-Ten : ',Tam.Ten);
 Writeln('-Diem : ',Tam.Diem);
 End;
{-------------------------------}
BEGIN
 TaoTapTin;
 Writeln;
 XemLaiBanGhi;
 Writeln;
 Write(' Bam <Enter>... ');
 Readln;
END.

.

Bài tập

 Program Tao_Danh_Sach;
Type
 HocSinh = RECORD
 Ten : String[7];
 Diem : 0..10;
 End;

 T_pList = ^T_List;
 T_List = RECORD
 d : HocSinh;
 Next : T_pList;
 End;
Var
 f : File Of Hocsinh;
 First : Pointer;
 Curr, News : T_pList;
{-------------------------------}
 Procedure MoTapTin;
 Var
 Filename : String;
 Begin
 Write('-Cho biet ten tap tin: ');
 Readln(Filename);
 Assign(f,Filename);
 {$I-}
 Reset(f);

 {$I+}
 If IOResult <> 0 Then
 Begin
 Writeln('Khong mo duoc tap tin: ',Filename);
 Halt;
 End;
 End;
{-------------------------------}
 Procedure DocVaoList;
 Begin
 First := Nil;
 While NOt EOF(f) Do
 Begin
 New(News);
 News^.Next := Nil;
 Read(f,News^.d);
 If First = Nil Then
 First := News
 Else
 Curr^.Next := News;
 Curr := News;
 End;
 Close(f);
 End;
{-------------------------------}
 Procedure Xem;
 Begin
 Curr :=First;
 While Curr <> Nil Do
 Begin
 Writeln('-Ten: ',Curr^.D.Ten : 6, #32:10,
 '-Diem : ',Curr^.D.Diem);
 Curr := Curr^.Next;
 End;
 End;
{-------------------------------}
BEGIN
 MoTapTin;
 Writeln;
 DocVaoList;
 Writeln;
 Xem;
 Writeln;
 Write(' Bam <Enter>... ');
 Readln;
END.

.

Bài tập

 Program Du_Doan_Bong_Da;
Type
 Doi = RECORD
 Diem,hlv,tm,hv,ct,sb : Real;
 Ten : String[24];
 Hang : Integer;
 End;
 Filedb = file Of Doi;
 Mang = Array[1..40] Of Doi;
Var
 f : Filedb;
 i,j,n : Integer;
 a : Mang;
 t : Doi;
 q : Boolean;
Begin
 i := 1;
 q := True;
 While q Do
 With a[i] Do
 Begin
 Write('-Ten doi (bam <Enter> de ngung): ');
 Readln(Ten);
 If Ten = '' Then
 q := False
 Else
 Begin
 Repeat
 Write('=Diem huan luyen vien: ');
 Readln(hlv);
 Until hlv <=30;
 Repeat
 Write('=Diem thu mon: ');
 Readln(tm);
 Until tm <= 15;
 Repeat
 Write('=Diem hau ve: ');
 Readln(hv);
 Until hv <= 30;
 Repeat
 Write('=Diem cac cau thu khac: ');
 Readln(ct);
 Until ct <= 50;
 Repeat
 Write('=Diem thuan loi san bai: ');
 Readln(sb);
 Until sb <= 20;
 Diem := hlv + hv + tm + ct + sb;
 i := i + 1;
 End;
 End;
 n := i - 1;
 For i := 1 To N - 1 Do
 For j := 1 To N - i Do
 If a[j].Diem < a[j+1].Diem then
 Begin
 t :=a[j];
 a[j] := a[j+1];
 a[j+1] := t;
 End;
 Assign(f,'diemdb.dat');
 Rewrite(f);
 For i := 1 to N Do
 Begin
 If (i > 1) And (a[i].Diem = a[i-1].Diem) Then
 a[i].Hang := a[i-1].Hang
 Else
 a[i].Hang := i;
 Write(f,a[i]);
 End;
 Close(f);
End.

.

Bài tập

 Program Cat_Tap_tin;
Var
 f,g1,g2 : File;
 Buf : Array[1..63000] Of Byte;
 Trungdiem : LongInt;
{-------------------------------------}
 Procedure BaoLoi;
 Begin
 Writeln('Khong mo duoc tap tin');
 Halt;
 End;
{-------------------------------------}
 Procedure MoTapTin;
 Var
 TenTT,TenTT1,TenTT2: String;
 Begin
 Write('-Ten tap tin nguon: ');
 Readln(TenTT);
 Write('-Ten tap tin dich 1: ');
 Readln(TenTT1);
 Write('-Ten tap tin dich 2: ');
 Readln(TenTT2);
 Assign(f,TenTT);
 Reset(f,1);
 Assign(g1,TenTT1);
 Rewrite(g1,1);
 Assign(g2,TenTT2);
 Rewrite(g2,1);
 If IOResult <> 0 Then
 BaoLoi;
 End;
{-------------------------------------}
 Procedure TinhTrungDiem;
 Begin
 TrungDiem := (Filesize(f) Div 2);
 End;
{-------------------------------------}
 Procedure ChepNuaDau;
 Var
 S : LongInt;
 Num,SoDoc,SoGhi : Word;
 Begin
 S :=TrungDiem;
 Repeat
 If Sizeof(Buf) <= S Then
 Num := Sizeof(Buf)
 Else
 Num := S;
 BlockRead(f,Buf, Num,SoDoc);
 If IOResult <> 0 Then
 BaoLoi;
 BlockWrite(g1,Buf,SoDoc,SoGhi);
 If IOResult <> 0 Then
 BaoLoi;
 Dec(S,Num);
 Until S = 0;
 Close(g1);
 End;
{-------------------------------------}
 Procedure ChepNuaSau;
 Var
 SoDoc,SoGhi : Word;
 Begin
 Seek(f,TrungDiem);
 If IOResult <> 0 Then
 BaoLoi;
 Repeat
 BlockRead(f,Buf, Sizeof(Buf),SoDoc);
 If IOResult <> 0 Then
 BaoLoi;
 BlockWrite(g2,Buf,SoDoc,SoGhi);
 If IOResult <> 0 Then
 BaoLoi;
 Until (SoDoc = 0) Or (SoGhi <> SoDoc);
 Close(g2);
 Close(f);
 End;
{-------------------------------------}
BEGIN
 MoTapTin;
 TinhTrungDiem;
 ChepNuaDau;
 ChepNuaSau;
 Writeln;
 Write('Da thuc hien xong, bam <Enter>... ');
 Readln;
END.

.

Bài tập

 Program Menu;
Uses Crt;
Type
 St17 = String[17];
 St7 = String[7];
 HoSo = RECORD
 Holot : St17;
 Ten : St7;
 ns : Integer;
 Diem : Real
 End;
 Mang = Array[1..100] Of HoSo;
 fhs = File Of HoSo;
Var
 Filename : String[11];
 f : fhs;
 Tam : HoSo;
 Ch : Char;
{----------------------------------}
 Procedure Nhap(Var f : fhs);
 Begin
 Rewrite(f);
 With Tam Do
 Repeat
 Write('-Ho lot (0 de ket thuc): ');
 Readln(Holot);
 If Holot <> '0' Then
 Begin
 Write('-Ten: ');
 Readln(Ten);
 Write('-Nam sinh: ');
 Readln(Ns);
 Write('-Diem: ');
 Readln(Diem);
 Write(f,tam);
 End;
 Until HoLot = '0';
 Close(f);
 End;
{----------------------------------}
 Procedure SapXep(Var f : Fhs);
 Var
 i,j,Spt : Integer;
 ds : Mang;
 Begin
 Reset(f);
 Spt := 0;
 While Not EOF(f) Do
 Begin
 Spt := Spt + 1;
 Read(f,ds[spt]);
 End;
 For i := 1 To spt - 1 Do
 For j := spt Downto i + 1 Do
 If ds[j].Ten[1] < ds[j-1].Ten Then
 Begin
 Tam := ds[j];
 ds[j] := ds[j-1];
 ds[j-1] := Tam;
 End;
 Rewrite(f);
 For i := 1 To spt Do
 Write(f,ds[i]);
 Close(f);
 Writeln;
 Write('Da sap xep xong, bam <Enter>... ');
 Readln;
 End;
{----------------------------------}
 Procedure Xem(Var f : Fhs);
 Begin
 ClrScr;
 Writeln(' HO VA TEN DIEM');
 Reset(f);
 While Not EOF(f) Do
 Begin
 Read(f,Tam);
 With Tam Do
 Writeln(Holot:17,' ',Ten:7,' ',Diem:6:1);
 End;
 Readln;
 End;
{----------------------------------}
 Procedure CapNhat(Var f : Fhs);

{--------------------}
 Procedure Sua(Var f:Fhs);
 Var
 Holot1 : St17;
 Ten1 : St7;
 TimThay : Boolean;
 Begin
 Repeat
 Write('-Holot: ');
 Readln(Holot1);
 Write('-Ten : ');
 Readln(Ten1);
 TimThay := False;
 Reset(f);
 While Not EOF(f) Do
 With Tam Do
 Begin
 Read(f,Tam);
 If (Holot = Holot1) And (Ten = Ten1) Then
 Begin
 Timthay := True;
 Writeln(Holot,' ',Ten,' Diem : ',Diem : 0:1);
 Repeat
 Writeln('Co sua khong ? (c/k) ');
 Ch := Readkey;
 Until Ch in['c','C','k','K'];
 If Upcase(Ch) = 'C' Then
 Begin
 Write('-Ho lot: ');
 Readln(Holot);
 Write('-Ten : ');
 Readln(Ten);
 Write('-Nam sinh : ',ns);
 Write('-Diem : ');
 Readln(Diem);
 Seek(f,filepos(f)-1);
 Write(f,Tam);
 End;
 End;
 End;
 If Not TimThay Then
 Writeln('Khong tim thay');
 Repeat
 Writeln('Tim nu khong ? (c/k) ');
 Ch := Readkey;
 Until Ch in['c','C','k','K'];
 Until Upcase(Ch) = 'K'
 End;
{--------------------}
 Procedure Them(Var f: Fhs);
 Begin
 Reset(f);
 Seek(f,Filesize(f));
 With Tam Do
 Repeat
 Write('-Ho lot: ');
 Readln(Holot);
 Write('-Ten : ');
 Readln(Ten);
 Write('-Nam sinh : ',ns);
 Write('-Diem : ');
 Readln(Diem);
 Write(f,Tam);
 Repeat
 Writeln('Them nua khong ? (c/k) ');
 Ch := Readkey;
 Until Ch in['c','C','k','K'];
 Until Upcase(Ch) = 'K';
 End;
{-------------------}
 Procedure Xoa(Var f : Fhs);
 Var
 ds : Mang;
 Holot1 : St17;
 Ten1 : St7;
 i,spt,vitri : Integer;
 TimThay : Boolean;
 Begin
 Reset(f);
 spt := 0;
 While Not EOF(f) Do
 Begin
 Read(f,Tam);
 spt := spt + 1;
 ds[spt] := Tam;
 End;
 Repeat
 Write('-Ho lot : ');
 Readln(holot1);
 Write('-Ten : ');
 Readln(Ten1);
 TimThay := False;
 i := 0;
 Repeat
 i := i + 1;
 If (ds[i].Holot = Holot1) And (ds[i].Ten = Ten1) Then
 Begin
 TimThay := True;
 vitri := i;
 End;
 Until TimThay Or (i > spt);
 If TimThay Then
 Begin
 With ds[vitri] Do
 Writeln(Holot,' ',Ten,' Diem: ',Diem:0:1);
 Repeat
 Writeln('Co xoa khong ? (c/k) ');
 Ch := Readkey;
 Until Ch in['c','C','k','K'];
 If Upcase(Ch) = 'C' Then
 Begin
 spt := spt - 1;
 For i := vitri To spt Do
 ds[i] := ds[i+1];
 End;
 End
 Else
 Writeln('Khong tim thay');
 Repeat
 Writeln('Tim nua khong ? (c/k) ');
 Ch := Readkey;
 Until Ch in['c','C','k','K'];
 Until Upcase(Ch) = 'K';
 Rewrite(f);
 For i := 1 To spt Do
 Write(f,ds[i]);
 Close(f);
 End;
 {-----Chuong trinh chiinh cua cap nhat-------}
 Begin
 Repeat
 Repeat
 ClrScr;
 Writeln(' MENU CAP NHAT ');
 Writeln('1-Sua');
 Writeln('2-Them');
 Writeln('3-Xoa');
 Writeln('4-Thoat');
 Ch := Readkey;
 Until Ch in['1'..'4'];
 Case Ch Of
 '1' : Sua(f);
 '2' : Them(f);
 '3' : Xoa(f);
 End;
 Until Ch = '4'
 End;
 {************ CHUONG TRINH CHINH ***********}
 BEGIN
 ClrScr;
 Write('-Ten tap tin : ');
 Readln(Filename);
 Assign(f,Filename);
 Repeat
 Repeat
 ClrScr;
 Writeln(' MENU CHINH');
 Writeln(' 1-Nhap');
 Writeln(' 2-Sap xep');
 Writeln(' 3-Xem');
 Writeln(' 4-Cap nhat');
 Writeln(' 5-Ket thuc');
 Writeln;
 Ch := Readkey;
 Until ch in['1'..'5'];
 Case Ch Of
 '1' : Nhap(f);
 '2' : SapXep(f);
 '3' : Xem(f);
 '4' : CapNhat(f);
 End;
 Until Ch = '5'
 END.

.

Bài tập

 Program D0_Dai_Cua_Dong;
Var
 f : Text;
 Filename : String[12];
 St : String;
 Max,Min: Integer;
 Sodong,Tong : Integer;
Begin
 Write('-Cho biet ten tap tin: ');
 Readln(Filename);
 Assign(f,Filename);
 Reset(f);
 Readln(f,St);
 Max := length(St);
 Min := Length(St);
 Sodong := 1;
 Tong := Length(St);
 While Not EOF(f) Do
 Begin
 Readln(f,St);
 If Max < Length(St) Then
 Max := Length(St);
 If Min > Length(St) Then
 Min := Length(St);
 Sodong := sodong + 1;
 Tong := Tong + Length(St);
 End;
 Writeln('-Dong dai nhat : ',Max);
 Writeln('-Dong ngan nhat : ',Min);
 Writeln('-Trung binh : ',Tong / Sodong : 6:1);
 Writeln;
 Write('Bam <Enter>... ');
 Readln
End.

.

Bài tập

 Program Diem_San_Pham;
Uses Crt;
Var
 f : Text;
 Nhom : Char;
 d1,d2 : Real;
 TongA1,TongA2 : Real;
 TongB1,TongB2 : Real;
 TongC1,TongC2 : Real;
 SoA,SoB,SoC : Integer;
 i : Integer;
Begin
 Assign(f,'sanpham.txt');
 Rewrite(f);
 Writeln(f,'Nhom nguoi',' San pham 1 ',' San pham 2 ');
 Writeln(f);
 ClrScr;
 Repeat
 Write('Nhom nguoi ($ de thoat): ');
 Readln(Nhom);
 If Nhom <> '$' Then
 Begin
 Write('-Diem san pham 1 : ');
 Readln(d1);
 Write('-Diem san pham 2 : ');
 Readln(d2);
 Writeln(f,Upcase(Nhom):6,d1:16:1,d2:16:1);
 End;
 Until Nhom = '$';
 Close(f);
 ClrScr;
 Reset(f);
 Readln(f);
 Readln(f);
 TongA1 := 0;TongA2 := 0;SoA := 0;
 TongB1 := 0;TongB2 := 0;SoB := 0;
 TongC1 := 0;TongC2 := 0;SoC := 0;
 While Not EOF(f) Do
 Begin
 For i := 1 To 6 Do {So vong lap bang vi tri cua nhom }
 Read(f,Nhom);
 Readln(f,d1,d2);
 Case Nhom Of
 'A' : Begin
 TongA1 := TongA1 + d1;
 TongA2 := TongA2 + d2;
 SoA := SoA + 1;
 End;
 'B' : Begin
 TongB1 := TongB1 + d1;
 TongB2 := TongB2 + d2;
 SoB := SoB + 1;
 End;
 'C' : Begin
 TongC1 := TongC1 + d1;
 TongC2 := TongC2 + d2;
 SoC := SoC + 1;
 End;
 End;
 End;
 ClrScr;
 Writeln('NHOM NGUOI',' TB San pham 1',' TB San pham 2');
 Writeln;
 If SoA <> 0 Then
 Writeln('A':6,TongA1/SoA:16:1,TongA2/SoA:16:1);
 If SoB <> 0 Then
 Writeln('B':6,TongB1/SoB:16:1,TongB2/SoB:16:1);
 If SoC <> 0 Then
 Writeln('C':6,TongC1/SoC:16:1,TongC2/SoC:16:1);
 Readln
End.

.

Bài tập

 Program DemChu;
Uses Crt;
Type
 MangChu = Array[Char] Of Integer;
Var
 f : Text;
 Filename : String;
 Line : String[25];
 Chu : Char;
 Letters,Lines,k : Integer;
 Dem : MangChu;
Begin
 ClrScr;
 For Chu := Chr(0) To Chr(127) Do
 Dem[chu] := 0;
 Letters := 0;
 Write('-Cho biet ten tap tin: ');
 Readln(Filename);
 Assign(f,Filename);
 Reset(f);
 While Not EOF(f) Do
 Begin
 Readln(f,Line);
 For k := 1 To Length(line) Do
 Begin
 If Line[k] In ['a'..'z'] Then
 Letters := Letters + 1;
 Dem[Line[k]] := Dem[Line[k]] + 1;
 End;
 End;
 Lines := 1;
 Close(f);
 Writeln('Tap tin: ',Filename,' co tat ca: ',Letters,' chu khong viet hoa');
 Writeln;
 Writeln('Phan phoi tan suat cua cac chu nhu sau:');
 Writeln;
 For Chu :='a' To 'z' Do
 Begin
 Write('-Chu: ',Chu,' = ');
 Write((Dem[chu]/Letters * 100):6:2,' % ');
 If (Lines Mod 4) = 0 Then
 Writeln;
 Lines := Lines + 1;
 End;
 Readln
End.

.

Bài tập

 Program Mo_Tap_Tin;
Label
 NhapTenTapTin;
Var
 F :Text;
 Filename : String;
{--------------------------------------}
 Procedure ThongBao(Str : String);
 Begin
 Write(Str + ' .Bam <Enter>');
 Readln;
 End;
{--------------------------------------}
BEGIN
 NhapTenTapTin:
 Writeln;
 Write('-Cho biet ten tap tin van ban can mo: ');
 Readln(Filename);
 {$I-}
 Assign(f,Filename);
 Reset(f);
 If IOResult = 0 Then
 ThongBao('Da mo tap tin: '+ Filename+' ')
 Else
 Begin
 Rewrite(f);
 If IOResult = 0 Then
 ThongBao('Da mo tap tin moi: '+Filename+' ')
 Else
 Begin
 ThongBao('Khong the mo tap tin: '+Filename+' ');
 Exit;
 End;
 End;
 Close(f);
END.

.

Bài tập

 Program Tao_Tap_Tin_Van_Ban;
Var
 f : Text;
 Filename : String;
{---------------------------------}
 Procedure Timvb(Var f: text; n : Word);
 Var
 i : Word;
 Begin
 Reset(f);
 For i :=1 To n Do
 Readln(f);
 End;
{---------------------------------}
 Procedure MoTapTin;
 Begin
 Write('-Cho biet ten tap tin van ban: ');
 Readln(Filename);
 {$I-}
 Assign(f,Filename);
 Rewrite(f);
 If IOResult <> 0 Then
 Begin
 Writeln('Khong the mo tap tin moi: '+Filename+' ');
 Halt;
 End;
 End;
{---------------------------------}
 Procedure Nhap4dong;
 Var
 Tam : String;
 i : Byte;
 Begin
 Writeln;
 Writeln;
 For i := 1 to 4 Do
 Begin
 Write('-Nhap dong thu: ',i:2,' : ');
 Readln(Tam);
 Writeln(f,Tam);
 End;
 End;
{---------------------------------}
 Procedure Xuatdong2;
 Var
 Tam : String;
 Begin
 Timvb(f,2);
 Readln(f,Tam);
 Writeln('Dong thu 3 cua tap tin co noi dung la: ');
 Writeln;
 Writeln(' ',Tam);
 End;
{---------------------------------}
BEGIN
 MoTapTin;
 Nhap4dong;
 Writeln;
 Xuatdong2;
 Writeln;
 Write(' Bam <Enter>... ');
 Readln;
END.

.

Bài tập

Program Xoa_Dong_Tap_Tin_Van_Ban;
Var
 f : Text;
 Filename : String;
{---------------------------------}
 Procedure MoTapTin;
 Var
 Tam : String;
 i : Byte;
 Begin
 Write('-Cho biet ten tap tin van ban: ');
 Readln(Filename);
 {$I-}
 Assign(f,Filename);
 Rewrite(f);
 {$I+}
 If IOResult <> 0 Then
 Begin
 Writeln('Khong the mo tap tin moi: '+Filename+' ');
 Halt;
 End;
 For i := 1 to 4 Do
 Begin
 Write('-Nhap dong thu: ',i:2,' : ');
 Readln(Tam);
 Writeln(f,Tam);
 End;
 Close(f);
 End;
{---------------------------------}
 Procedure XemTapTin(Var f : Text);
 Var
 Tam : String;
 Begin
 Reset(f);
 While Not EOF(f) Do
 Begin
 Readln(f,Tam);
 Writeln(Tam);
 End;
 End;
{---------------------------------}
 Procedure Xoadong(Var f : Text; n : Word);
 Var
 g : Text;
 Tam : String;
 i : Word;
 Begin
 Assign(g,Filename);
 Reset(g);
 Assign(f,'XOADONG.TXT');
 Rewrite(f);
 i := 0;
 While Not EOF(g) Do
 Begin
 Readln(g,Tam);
 If i <> n Then
 Writeln(f,Tam);
 Inc(i);
 End;
 Close(f);
 End;
{---------------------------------}

BEGIN
 MoTapTin;
 Writeln;
 Writeln(' Noi dung tap tin da tao');
 Writeln;
 XemTaptin(f);
 Writeln;
 Xoadong(f,2);
 Writeln(' Noi dung con lai sau khi xoa dong 3');
 Writeln;
 XemTapTin(f);
 Writeln;
 Write(' Bam <Enter>... ');
 Readln;
END.

.

Bài tập

 Program Xoa_chu_thich;
Var
 Filename : String;
 f,fn : Text;
 Ch : Char;
Begin
 Write('-Ten tap tin Pascal: ');
 Readln(Filename);
 Assign(f,Filename);
 Assign(fn,'new.pas');
 reset(f);
 Rewrite(fn);
 While not EOF(f) Do
 Begin
 Read(f,ch);
 If Ch <> '{' Then
 Write(fn,ch)
 Else
 Repeat
 Read(f,ch);
 Until (Ch = '}') Or EOF(f);
 End;
 Close(f);
 Close(fn);
 Writeln;
 Write('Da thuc hien xong, bam <Enter>... ');
 Readln;
End.

.

Bài tập

 Program Tim_Chuoi_Ky_Tu;
Var
 Filename : String[12];
 f : Text;
 St : String;
 Ch : Char;
 Ok : Boolean;
 i,solan:Integer;
Begin
 Write('-Ten tap tin: ');
 Readln(Filename);
 Write('-Nhap chuoi ky tu: ');
 Readln(St);
 Assign(f,Filename);
 Reset(f);
 Solan := 0;
 While NOt EOF(f) Do
 Begin
 Read(f,Ch);
 If ch = St[1] Then
 Begin
 Ok := True;
 i := 1;
 While Not OK And ( i < length(St)) Do
 Begin
 Read(f,Ch);
 If (Ch <> Chr(10)) And (Ch <> Chr(13)) Then
 If Ch = St[1] Then
 i := 1
 Else
 Begin
 i := i + 1;
 If (Ch <> St[i]) Then
 Ok := False;
 End;
 End;
 If Ok Then
 Solan := Solan + 1;
 End;
 End;
 Write('-Chuoi: ',St,' xuat hien : ',solan,' lan trong tap tin');
 Readln;
 Close(f);
End.

.

Bài tập

 Program Xu_ly_dong;
Var
 f1,f2 : Text;
 Filename : String[12];
 lmax : Integer;
 Tam,st,dong : String;
{--------------------------------------}
 Procedure Catdong(Var st,dong:String;lmax :Integer);
 Var
 i : Integer;
 Begin
 i := lmax;
 While st[i] <> ' ' Do
 i:= i-1;
 Dong := copy(st,1,i-1);
 Delete(St,1,i);
 End;
{--------------------------------------}
 Procedure Lamday(Var dong: String;lmax : Integer);
 Var
 i,j : Integer;
 Begin
 i := lmax - length(dong);
 While i <> 0 Do
 Begin
 j := Length(dong);
 While (j > 1) And (i <> 0) Do
 If (dong[j]=' ') And (dong[j-1] <> ' ') Then
 Begin
 Insert(' ',dong,j);
 j :=j-1;
 i := i-1;
 End
 Else
 j := j-1;
 End;
 End;
{--------------------------------------}
BEGIN
 Write('-Ten tap tin: ');
 Readln(Filename);
 Write('-Chieu dai cua dong: ');
 Readln(lmax);
 Assign(f1,filename);
 Reset(f1);
 Assign(f2,'new.txt');
 Rewrite(f2);
 St:=' ';
 While NOt EOF(f1) Do
 Begin
 Readln(f1,tam);
 St := St + Tam + ' ';
 While length(St) >= lmax Do
 Begin
 Catdong(St,dong,lmax);
 Lamday(dong,lmax);
 Writeln(f2,dong);
 End;
 End;
 Writeln(f2,St);
 Writeln;
 Writeln('Da thuc hien xong, bam <Enter>... ');
 Readln;
 reset(f2);
 While Not EOF(f2) Do
 Begin
 Readln(f2,dong);
 Writeln(dong);
 End;
 Writeln;
 Write(' Xem xong bam <Enter>... ');
 Readln;
 Close(f1);
 Close(f2);
END.

.

Bài tập


.

Advertisements

About pascalteacher

Trang thông tin Toán học và Tin học

Thảo luận

Không có bình luận

Trả lời

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Đăng xuất / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Đăng xuất / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Đăng xuất / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Đăng xuất / Thay đổi )

Connecting to %s

Các tác giả

Categories

Tháng Mười 2014
H B T N S B C
« Th11   Th11 »
 12345
6789101112
13141516171819
20212223242526
2728293031  

NCT Computer

Flickr Photos

Thống kê

  • 209,568 lượt xem

pascalteacher.nct@gmail.com


Trang huấn luyện học sinh giỏi Tin học

%d bloggers like this: