Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pascal Programming Today Chapter 11 1 Chapter 11.

Similar presentations


Presentation on theme: "Pascal Programming Today Chapter 11 1 Chapter 11."— Presentation transcript:

1 Pascal Programming Today Chapter 11 1 Chapter 11

2 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.

3 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.

4 Pascal Programming Today Chapter 11 4 »Procedures involved in writing data to files: –assign –rewrite –write –close

5 Pascal Programming Today Chapter 11 5 »Procedures involved in reading data from files: –assign –reset –read –close

6 Pascal Programming Today Chapter 11 6 »Variables of type text are used to identify files. Syntax of declaring text files var : text;

7 Pascal Programming Today Chapter 11 7 »Procedure assign associates file variables with files in secondary storage. Syntax of the procedure assign assign(, ) It is a user-defined identifier declared as type text.

8 Pascal Programming Today Chapter 11 8 »Procedure assign associates file variables with files in secondary storage. Syntax of the procedure assign assign(, ) May be a string constant of the file name or a string variable holding the file name.

9 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.

10 Pascal Programming Today Chapter 11 10 Syntax of the procedure rewrite rewrite( )

11 Pascal Programming Today Chapter 11 11 »Procedure reset prepares files for reading data from them. Syntax of the procedure reset reset( )

12 Pascal Programming Today Chapter 11 12 Differences between procedures rewrite and reset

13 Pascal Programming Today Chapter 11 13 »Procedure close is used to close a file. Syntax of the procedure close close( )

14 Pascal Programming Today Chapter 11 14 Summary of procedures of writing data to and reading data from a file

15 Pascal Programming Today Chapter 11 15 »Procedures writeln and write can save data to files.

16 Pascal Programming Today Chapter 11 16 Syntax of the procedures writeln and write for writing data to files writeln(,,,...) or write(,,,...)

17 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.

18 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.

19 Pascal Programming Today Chapter 11 19 »Procedures readln and read can obtain data from files.

20 Pascal Programming Today Chapter 11 20 Syntax of the procedures readln and read for reading data from files readln(,,,...) or read(,,,...)

21 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.

22 Pascal Programming Today Chapter 11 22 Results of reading variables of different widths and types

23 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.

24 Pascal Programming Today Chapter 11 24 Assume the contents of a file ‘Exampl1_6.txt’ is 210  220  230  240  250  260  270.5 

25 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.

26 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.

27 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.

28 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.

29 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.

30 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.

31 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.

32 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.

33 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.

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

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

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

37 Pascal Programming Today Chapter 11 37 –edit data of the file (i.e. modify the data)


Download ppt "Pascal Programming Today Chapter 11 1 Chapter 11."

Similar presentations


Ads by Google