pascal programming today chapter 11 1 chapter 11

Post on 17-Jan-2016

270 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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.

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.

Pascal Programming Today Chapter 11

4

» Procedures involved in writing data to files:–assign

–rewrite

–write

–close

Pascal Programming Today Chapter 11

5

» Procedures involved in reading data from files:–assign

–reset

–read

–close

Pascal Programming Today Chapter 11

6

» Variables of type text are used to identify files.

Syntax of declaring text files

var

<File variable> : text;

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.

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.

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.

Pascal Programming Today Chapter 11

10

Syntax of the procedure rewrite

rewrite(<File variable>)

Pascal Programming Today Chapter 11

11

» Procedure reset prepares files for reading data from them.

Syntax of the procedure reset

reset(<File variable>)

Pascal Programming Today Chapter 11

12

Differences between procedures rewrite and reset

Pascal Programming Today Chapter 11

13

» Procedure close is used to close a file.

Syntax of the procedure close

close(<File variable>)

Pascal Programming Today Chapter 11

14

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

Pascal Programming Today Chapter 11

15

» Procedures writeln and write can save data to files.

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>, ...)

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.

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.

Pascal Programming Today Chapter 11

19

» Procedures readln and read can obtain data from files.

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>, ...)

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.

Pascal Programming Today Chapter 11

22

Results of reading variables of different widths and types

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.

Pascal Programming Today Chapter 11

24

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

210220230240250260270.5

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.

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.

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.

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.

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.

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.

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.

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.

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.

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>)

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)

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)

Pascal Programming Today Chapter 11

37

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

top related