Pascal Programming Today Chapter 11 1 Chapter 11.

37
Pascal Programming Today Chapte r 11 1 Chapter 11

Transcript of Pascal Programming Today Chapter 11 1 Chapter 11.

Page 1: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

1

Chapter 11

Page 2: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

2

» Data are usually stored in text files.

» Text files save data one by one in the format of a sequence of characters.

Page 3: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

3

» Each line in a text file is ended with an end-of-line marker.

» An end-of-file marker, indicates the end of the file, is placed at the end of the file.

Page 4: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

4

» Procedures involved in writing data to files:–assign

–rewrite

–write

–close

Page 5: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

5

» Procedures involved in reading data from files:–assign

–reset

–read

–close

Page 6: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

6

» Variables of type text are used to identify files.

Syntax of declaring text files

var

<File variable> : text;

Page 7: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

7

» Procedure assign associates file variables with files in secondary storage.

Syntax of the procedure assign

assign(<File variable>, <File name>)

It is a user-defined identifier declared as type text.

Page 8: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

8

» Procedure assign associates file variables with files in secondary storage.

Syntax of the procedure assign

assign(<File variable>, <File name>)

May be a string constant of the file name or a string variable holding the file name.

Page 9: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

9

» Procedure rewrite prepares files for writing data to them.

» Original data will be lost when procedure rewrite is applied to the files.

Page 10: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

10

Syntax of the procedure rewrite

rewrite(<File variable>)

Page 11: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

11

» Procedure reset prepares files for reading data from them.

Syntax of the procedure reset

reset(<File variable>)

Page 12: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

12

Differences between procedures rewrite and reset

Page 13: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

13

» Procedure close is used to close a file.

Syntax of the procedure close

close(<File variable>)

Page 14: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

14

Summary of procedures of writing data to and reading data from a file

Page 15: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

15

» Procedures writeln and write can save data to files.

Page 16: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

16

Syntax of the procedures writeln and write for writing data to files

writeln(<File variable>, <Output item 1>, <Output item 2>, ...)

or

write(<File variable>, <Output item 1>, <Output item 2>, ...)

Page 17: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

17

» File variables must be supplied in front of the output lists, otherwise data will be displayed on screen only.

» Data written to files can be formatted in similar way as data displayed on screen.

Page 18: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

18

» Procedure writeln will add an end-of-line marker after a list of output items to files while procedure write will not.

Page 19: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

19

» Procedures readln and read can obtain data from files.

Page 20: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

20

Syntax of the procedures readln and read for reading data from files

readln(<File variable>, <Variable 1>, <Variable 2>, ...)

or

read(<File variable>, <Variable 1>, <Variable 2>, ...)

Page 21: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

21

» File variables must be supplied in front of the variable lists, otherwise data will be read from keyboard.

» Result of reading depends on the maximum width and the types of the variables.

Page 22: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

22

Results of reading variables of different widths and types

Page 23: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

23

» Procedure readln will advance the pointer after the end-of-line marker after reading while procedure read will advance to pass the read data only.

Page 24: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

24

Assume the contents of a file ‘Exampl1_6.txt’ is

210220230240250260270.5

Page 25: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

25

var S : string[5]; T, U : string[2]; C, D : char; M, N : integer; R : real; FileVariable : text;

Output

After the execution of the above program segment, the contents of the variables are as shown below.

Page 26: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

26

begin read(FileVariable, S, T); readln(FileVariable, C); read(FileVariable, M); readln(FileVariable,U);

Output

After the execution of the above program segment, the contents of the variables are as shown below.

Page 27: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

27

begin read(FileVariable, S, T); readln(FileVariable, C); read(FileVariable, M); readln(FileVariable,U);

Output

After the execution of the above program segment, the contents of the variables are as shown below.

Page 28: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

28

begin read(FileVariable, S, T); readln(FileVariable, C); read(FileVariable, M); readln(FileVariable,U);

Output

After the execution of the above program segment, the contents of the variables are as shown below.

Page 29: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

29

begin read(FileVariable, S, T); readln(FileVariable, C); read(FileVariable, M); readln(FileVariable,U);

Output

After the execution of the above program segment, the contents of the variables are as shown below.

Page 30: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

30

begin read(FileVariable, S, T); readln(FileVariable, C); read(FileVariable, M); readln(FileVariable,U);

Output

After the execution of the above program segment, the contents of the variables are as shown below.

Page 31: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

31

begin readln(FileVariable, C); read(FileVariable, M); readln(FileVariable,U); readln(FileVariable, D);

Output

After the execution of the above program segment, the contents of the variables are as shown below.

Page 32: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

32

begin read(FileVariable, M); readln(FileVariable,U); readln(FileVariable, D); read(FileVariable, N);

Output

After the execution of the above program segment, the contents of the variables are as shown below.

Page 33: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

33

begin readln(FileVariable,U); readln(FileVariable, D); read(FileVariable, N); readln(FileVariable, R);

Output

After the execution of the above program segment, the contents of the variables are as shown below.

Page 34: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

34

» The eof function returns the value TRUE if the pointer is currently pointing at the end-of-file marker; otherwise FASLE.

Syntax of the eof function

eof(<File variable>)

Page 35: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

35

» To update a sequential file means to – append data to the file

(i.e. add data to the end of the file)

Page 36: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

36

– insert data to the file(i.e. add data to the proper position of the file so that the order of the data can be maintained)

– delete data from the file(i.e. remove data from the file)

Page 37: Pascal Programming Today Chapter 11 1 Chapter 11.

Pascal Programming Today Chapter 11

37

– edit data of the file(i.e. modify the data)