overview - kam.mff.cuni.czperm/programovani/old/... · defining your own data type...

66
Defining your own Data Type Compiler-directives Files Overview Defining our own data-types (enumerated data-types), Control structures case ... of ..., Basic sorting algorithms, Compiler directives, Files (text files), Basic sorting algorithms. Martin Pergel, [email protected] Programov´ an´ ıI

Upload: others

Post on 30-Sep-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Overview

Defining our own data-types (enumerated data-types),

Control structures case ... of ...,

Basic sorting algorithms,

Compiler directives,

Files (text files),

Basic sorting algorithms.

Martin Pergel, [email protected]

Programovanı I

Page 2: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

How to pass an array as a parameter?

In Classical Pascal we have to define our own data-type(show why the naive approach does not work).

Turbo Pascal (and also Free Pascal) support open-arrayparameters.

Martin Pergel, [email protected]

Programovanı I

Page 3: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Defining your own data type:

Keyword type permits us to define a new data-type.

Martin Pergel, [email protected]

Programovanı I

Page 4: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Defining your own data type:

Keyword type permits us to define a new data-type.

trivial use: type int=integer;

Martin Pergel, [email protected]

Programovanı I

Page 5: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Defining your own data type:

Keyword type permits us to define a new data-type.

trivial use: type int=integer;

use: type x=array[1..10] of integer;

Martin Pergel, [email protected]

Programovanı I

Page 6: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Example

program nnn;

type arr=array[1..10] of integer;

var p:arr;

procedure output(a:arr);

var i:integer;

begin

for i:=1 to 10 do

writeln(a[i]);

end;

begin

...output(p);

end.

Martin Pergel, [email protected]

Programovanı I

Page 7: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Open-array parameters

Available in Turbo Pascal and Free Pascal.

Martin Pergel, [email protected]

Programovanı I

Page 8: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Open-array parameters

Available in Turbo Pascal and Free Pascal.

We say that the argument is an array of a particular type, butwe omit limits.

Martin Pergel, [email protected]

Programovanı I

Page 9: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Open-array parameters

Available in Turbo Pascal and Free Pascal.

We say that the argument is an array of a particular type, butwe omit limits.

Example: procedure output(a:array of integer);

Martin Pergel, [email protected]

Programovanı I

Page 10: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Open-array parameters

Available in Turbo Pascal and Free Pascal.

We say that the argument is an array of a particular type, butwe omit limits.

Example: procedure output(a:array of integer);

The argument is an array indexed from 0 to N.

Martin Pergel, [email protected]

Programovanı I

Page 11: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Open-array parameters

Available in Turbo Pascal and Free Pascal.

We say that the argument is an array of a particular type, butwe omit limits.

Example: procedure output(a:array of integer);

The argument is an array indexed from 0 to N.

The value N can be determined using a function high.

Martin Pergel, [email protected]

Programovanı I

Page 12: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Example using open-array parameters

procedure output(a:array of integer);

var i:integer;

begin

for i:=0 to high(a) do

writeln(a[i]);

end;

Martin Pergel, [email protected]

Programovanı I

Page 13: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Further possibilitieshow to use user-defined data-types

We want to calculate the days of a week. How we do that?

We define constants: Monday=1, Tuesday=2,...

Martin Pergel, [email protected]

Programovanı I

Page 14: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Further possibilitieshow to use user-defined data-types

We want to calculate the days of a week. How we do that?

We define constants: Monday=1, Tuesday=2,...

But I’ll change the numbering: Monday=0, Tuesday=1,...

Martin Pergel, [email protected]

Programovanı I

Page 15: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Further possibilitieshow to use user-defined data-types

We want to calculate the days of a week. How we do that?

We define constants: Monday=1, Tuesday=2,...

But I’ll change the numbering: Monday=0, Tuesday=1,...

Then an American comes and enumerates: Sunday=1,Monday=2,...

Martin Pergel, [email protected]

Programovanı I

Page 16: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Further possibilitieshow to use user-defined data-types

We want to calculate the days of a week. How we do that?

We define constants: Monday=1, Tuesday=2,...

But I’ll change the numbering: Monday=0, Tuesday=1,...

Then an American comes and enumerates: Sunday=1,Monday=2,...

Thus we define a special data-type indexed with days of aweek,

Martin Pergel, [email protected]

Programovanı I

Page 17: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Further possibilitieshow to use user-defined data-types

We want to calculate the days of a week. How we do that?

We define constants: Monday=1, Tuesday=2,...

But I’ll change the numbering: Monday=0, Tuesday=1,...

Then an American comes and enumerates: Sunday=1,Monday=2,...

Thus we define a special data-type indexed with days of aweek,

the numbers get assigned by the compiler.

Martin Pergel, [email protected]

Programovanı I

Page 18: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Enumerated data-type

Gets defined in the type-section,

Martin Pergel, [email protected]

Programovanı I

Page 19: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Enumerated data-type

Gets defined in the type-section,

individual values are in the brackets separated by commas.

Martin Pergel, [email protected]

Programovanı I

Page 20: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Enumerated data-type

Gets defined in the type-section,

individual values are in the brackets separated by commas.

Example: type daysofweek=(monday,tuesday,

wednesday, thursday,friday, saturday, sunday);

Martin Pergel, [email protected]

Programovanı I

Page 21: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Enumerated data-type

Gets defined in the type-section,

individual values are in the brackets separated by commas.

Example: type daysofweek=(monday,tuesday,

wednesday, thursday,friday, saturday, sunday);

Or we may directly define a variable of enumerated type:var cal:(monday,tuesday,wednesday,thursday,

friday, saturday,sunday);

Martin Pergel, [email protected]

Programovanı I

Page 22: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Example

Let us implement a simple ”calendar” for the year 2013, i.e.,we output the date and day of week.

Martin Pergel, [email protected]

Programovanı I

Page 23: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Example

Let us implement a simple ”calendar” for the year 2013, i.e.,we output the date and day of week.

For the sake of simplicity let’s consider that each monthconsists of 30 days...

Martin Pergel, [email protected]

Programovanı I

Page 24: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Example

Let us implement a simple ”calendar” for the year 2013, i.e.,we output the date and day of week.

For the sake of simplicity let’s consider that each monthconsists of 30 days...

Source code can be found on the web(kam.mff.cuni.cz/˜perm/programovani/NPRG030/enum.pas).

Martin Pergel, [email protected]

Programovanı I

Page 25: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Example

Let us implement a simple ”calendar” for the year 2013, i.e.,we output the date and day of week.

For the sake of simplicity let’s consider that each monthconsists of 30 days...

Source code can be found on the web(kam.mff.cuni.cz/˜perm/programovani/NPRG030/enum.pas).

We see that the write function cannot output enumerateddata-types.

Martin Pergel, [email protected]

Programovanı I

Page 26: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Example

Let us implement a simple ”calendar” for the year 2013, i.e.,we output the date and day of week.

For the sake of simplicity let’s consider that each monthconsists of 30 days...

Source code can be found on the web(kam.mff.cuni.cz/˜perm/programovani/NPRG030/enum.pas).

We see that the write function cannot output enumerateddata-types.

What should we do in order to write out the names of thedays?

Martin Pergel, [email protected]

Programovanı I

Page 27: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Example

Let us implement a simple ”calendar” for the year 2013, i.e.,we output the date and day of week.

For the sake of simplicity let’s consider that each monthconsists of 30 days...

Source code can be found on the web(kam.mff.cuni.cz/˜perm/programovani/NPRG030/enum.pas).

We see that the write function cannot output enumerateddata-types.

What should we do in order to write out the names of thedays?

Either we use large if-clause or case variable of ...

Martin Pergel, [email protected]

Programovanı I

Page 28: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Structure case ... of ...

It helps us to create many branches (in a program) dependingon the value of a variable.Syntax:case variable name of

value1: statement or blokvalue2: statement or blokelse statement or blok

end;

Only the branch labeled by current value of the variable getsexecuted. The else-branch gets executed otherwise (for othervalues).

The else-clause is not compulsory!

If the last clause is a block, we write the keyword end twice(the former closes the block, the latter finishes the case block.

Martin Pergel, [email protected]

Programovanı I

Page 29: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Example – calendar

can be found atkam.mff.cuni.cz/~perm/programovani/NPRG030/case of.pas.

Martin Pergel, [email protected]

Programovanı I

Page 30: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Compiler-directives

Compiler tests many issues, e.g.:

Martin Pergel, [email protected]

Programovanı I

Page 31: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Compiler-directives

Compiler tests many issues, e.g.:

whether we are not violating array boundaries,

Martin Pergel, [email protected]

Programovanı I

Page 32: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Compiler-directives

Compiler tests many issues, e.g.:

whether we are not violating array boundaries,

whether the stack does not overflow,

Martin Pergel, [email protected]

Programovanı I

Page 33: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Compiler-directives

Compiler tests many issues, e.g.:

whether we are not violating array boundaries,

whether the stack does not overflow,

whether the input/output error occured...

Martin Pergel, [email protected]

Programovanı I

Page 34: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Compiler-directives

Compiler tests many issues, e.g.:

whether we are not violating array boundaries,

whether the stack does not overflow,

whether the input/output error occured...

Usually it is a good idea to keep these tests switched on butsometimes we ”know what we are doing”.

Martin Pergel, [email protected]

Programovanı I

Page 35: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Compiler-directives

Compiler tests many issues, e.g.:

whether we are not violating array boundaries,

whether the stack does not overflow,

whether the input/output error occured...

Usually it is a good idea to keep these tests switched on butsometimes we ”know what we are doing”.

Then we can switch them off (but only if it is essential).

Martin Pergel, [email protected]

Programovanı I

Page 36: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Compiler-directives

Compiler tests many issues, e.g.:

whether we are not violating array boundaries,

whether the stack does not overflow,

whether the input/output error occured...

Usually it is a good idea to keep these tests switched on butsometimes we ”know what we are doing”.

Then we can switch them off (but only if it is essential).

We can do that using the compiler-directives.

Martin Pergel, [email protected]

Programovanı I

Page 37: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Compiler-directives

Compiler tests many issues, e.g.:

whether we are not violating array boundaries,

whether the stack does not overflow,

whether the input/output error occured...

Usually it is a good idea to keep these tests switched on butsometimes we ”know what we are doing”.

Then we can switch them off (but only if it is essential).

We can do that using the compiler-directives.

These directives look like a comment, i.e., they are in thebraces,

Martin Pergel, [email protected]

Programovanı I

Page 38: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Compiler-directives

Compiler tests many issues, e.g.:

whether we are not violating array boundaries,

whether the stack does not overflow,

whether the input/output error occured...

Usually it is a good idea to keep these tests switched on butsometimes we ”know what we are doing”.

Then we can switch them off (but only if it is essential).

We can do that using the compiler-directives.

These directives look like a comment, i.e., they are in thebraces,

just the ”comment” begins with the string-character ($).Then we place (usually 1-character long) name and a switch+/−.

Martin Pergel, [email protected]

Programovanı I

Page 39: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Compiler-directives

Example: {$R−} – switch the range-checking off.

Martin Pergel, [email protected]

Programovanı I

Page 40: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Compiler-directives

Example: {$R−} – switch the range-checking off.

The most important:

Martin Pergel, [email protected]

Programovanı I

Page 41: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Compiler-directives

Example: {$R−} – switch the range-checking off.

The most important:

$Q – overflow-checking,

Martin Pergel, [email protected]

Programovanı I

Page 42: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Compiler-directives

Example: {$R−} – switch the range-checking off.

The most important:

$Q – overflow-checking,$R – range-checking,

Martin Pergel, [email protected]

Programovanı I

Page 43: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Compiler-directives

Example: {$R−} – switch the range-checking off.

The most important:

$Q – overflow-checking,$R – range-checking,$I – input-output tests,

Martin Pergel, [email protected]

Programovanı I

Page 44: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Compiler-directives

Example: {$R−} – switch the range-checking off.

The most important:

$Q – overflow-checking,$R – range-checking,$I – input-output tests,The full list can be found in the manual (some directives arecompiler-dependent).

Martin Pergel, [email protected]

Programovanı I

Page 45: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Filesand functions related to them

This time we show handling of text files (binary files appearlater).

Martin Pergel, [email protected]

Programovanı I

Page 46: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Filesand functions related to them

This time we show handling of text files (binary files appearlater).

A text file is represented by a variable of type Text.

Martin Pergel, [email protected]

Programovanı I

Page 47: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Filesand functions related to them

This time we show handling of text files (binary files appearlater).

A text file is represented by a variable of type Text.

This variable gets assigned to a given file by theAssign-function,

Martin Pergel, [email protected]

Programovanı I

Page 48: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Filesand functions related to them

This time we show handling of text files (binary files appearlater).

A text file is represented by a variable of type Text.

This variable gets assigned to a given file by theAssign-function,

then we open the file using Reset, Rewrite or Append,

Martin Pergel, [email protected]

Programovanı I

Page 49: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Filesand functions related to them

This time we show handling of text files (binary files appearlater).

A text file is represented by a variable of type Text.

This variable gets assigned to a given file by theAssign-function,

then we open the file using Reset, Rewrite or Append,

after that we read (using Read and Readln functions). Thistime we give the Text-type variable as the first argument,

Martin Pergel, [email protected]

Programovanı I

Page 50: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Filesand functions related to them

This time we show handling of text files (binary files appearlater).

A text file is represented by a variable of type Text.

This variable gets assigned to a given file by theAssign-function,

then we open the file using Reset, Rewrite or Append,

after that we read (using Read and Readln functions). Thistime we give the Text-type variable as the first argument,

writing into the file is done in the same way by calling Write

or Writeln functions.

Martin Pergel, [email protected]

Programovanı I

Page 51: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Filesand functions related to them

This time we show handling of text files (binary files appearlater).

A text file is represented by a variable of type Text.

This variable gets assigned to a given file by theAssign-function,

then we open the file using Reset, Rewrite or Append,

after that we read (using Read and Readln functions). Thistime we give the Text-type variable as the first argument,

writing into the file is done in the same way by calling Write

or Writeln functions.

Finally we close the file using the Close-function.

Martin Pergel, [email protected]

Programovanı I

Page 52: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Filesand functions related to them – syntax (1)

var f:Text;

Martin Pergel, [email protected]

Programovanı I

Page 53: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Filesand functions related to them – syntax (1)

var f:Text;

Assign(f,’file.txt’); – assing the variable f withfile.txt.

Martin Pergel, [email protected]

Programovanı I

Page 54: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Filesand functions related to them – syntax (1)

var f:Text;

Assign(f,’file.txt’); – assing the variable f withfile.txt.

Reset(f); – open the file represented by f (for reading).

Martin Pergel, [email protected]

Programovanı I

Page 55: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Filesand functions related to them – syntax (1)

var f:Text;

Assign(f,’file.txt’); – assing the variable f withfile.txt.

Reset(f); – open the file represented by f (for reading).

Rewrite(f); – open f if it exists, destroy (erase) its contain.

Martin Pergel, [email protected]

Programovanı I

Page 56: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Filesand functions related to them – syntax (1)

var f:Text;

Assign(f,’file.txt’); – assing the variable f withfile.txt.

Reset(f); – open the file represented by f (for reading).

Rewrite(f); – open f if it exists, destroy (erase) its contain.

Append(f); – open f for appending (writing behind itscurrent end).

Martin Pergel, [email protected]

Programovanı I

Page 57: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Filesand functions related to them – syntax (2)

Writeln(f,’We are writing to the file’); – outputthe text into the file.

Martin Pergel, [email protected]

Programovanı I

Page 58: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Filesand functions related to them – syntax (2)

Writeln(f,’We are writing to the file’); – outputthe text into the file.

Read(f,a); – Read from the file variable a.

Martin Pergel, [email protected]

Programovanı I

Page 59: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Filesand functions related to them – syntax (2)

Writeln(f,’We are writing to the file’); – outputthe text into the file.

Read(f,a); – Read from the file variable a.

Close(f); – Close the file (we won’t use it anymore).

Martin Pergel, [email protected]

Programovanı I

Page 60: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Filesand functions related to them – syntax (2)

Writeln(f,’We are writing to the file’); – outputthe text into the file.

Read(f,a); – Read from the file variable a.

Close(f); – Close the file (we won’t use it anymore).

eof(f); – function returning boolean depending on whetherwe are (already) at the end of the file.

Martin Pergel, [email protected]

Programovanı I

Page 61: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Filesand functions related to them – syntax (2)

Writeln(f,’We are writing to the file’); – outputthe text into the file.

Read(f,a); – Read from the file variable a.

Close(f); – Close the file (we won’t use it anymore).

eof(f); – function returning boolean depending on whetherwe are (already) at the end of the file.

eof; – function announcing the end of standard input(usually from keyboard).

Martin Pergel, [email protected]

Programovanı I

Page 62: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Filesand functions related to them – syntax (2)

Writeln(f,’We are writing to the file’); – outputthe text into the file.

Read(f,a); – Read from the file variable a.

Close(f); – Close the file (we won’t use it anymore).

eof(f); – function returning boolean depending on whetherwe are (already) at the end of the file.

eof; – function announcing the end of standard input(usually from keyboard).

There are many further function Rename, Erase,...

Martin Pergel, [email protected]

Programovanı I

Page 63: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Problems with files

It can happen that a file we try to open with Reset does notexist.

Martin Pergel, [email protected]

Programovanı I

Page 64: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Problems with files

It can happen that a file we try to open with Reset does notexist.

This causes an input/output error.

Martin Pergel, [email protected]

Programovanı I

Page 65: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Problems with files

It can happen that a file we try to open with Reset does notexist.

This causes an input/output error.

To avoid this we can either destroy the file (calling Rewrite –this always creates a file): but this is usually verycounter-productive! Alternatively, we use an appropriatecompiler-directive (to switch the input/output error off) and ifan error occurs, we find out about it by calling theIOResult-function.

Martin Pergel, [email protected]

Programovanı I

Page 66: Overview - kam.mff.cuni.czperm/programovani/old/... · Defining your own Data Type Compiler-directives Files Open-array parameters Available in Turbo Pascal and Free Pascal. We say

Defining your own Data Type Compiler-directives Files

Example

Assign(f,’file.txt’);

{$I−} {Switch the tests on input/output errors off}Reset(f);

{$I+} {Switch IO-error on again}if IOResult<>0 then

begin writeln(’A problem!’); halt;

end;

while not eof(f) do begin

readln(f,s);

writeln(s);

end;

Beware that IOResult is a function and thus after calling it, theerror-value gets replaced by 0. Thus we have to store it into avariable (for further use).

Martin Pergel, [email protected]

Programovanı I