Thursday, January 26, 2017

Lecture 12 - Pascal Programming (Part VI)



File Processing
pascal provides a simple set of functions reading and writing data into external storage devices. A collection of data written into a storage is called a file. There are two types of files used in Pascal. All files require an internal identifier which can be declared as below.

Text Files
These are a series of characters written into a file. These files are human readable and uses ASCII text characters.

VAR
Myfilel : TEXT; { identifier for a text file )


Binary Files
These are blocks of data that is written into a file. These files cannot be displayed without using an appropriate program to read and display them.

VAR
DataFilel : FILE of integer;
DataFile2 : FILE of StuCentRecord; { identifier for a binary file )


File Handling Functions

Assign( DataFilel, 'Stud.Dat') - This function is used to assign an internal file identifier to an external filename.

Reset(DataFilel ) - Opens the file for reading data

Rewrite(DataFilel) - reates a new file for writing data. lf used on existing one the data get erased.

Append(DataFilel ) - Opens an existing file for adding new records to the bottom.

Close(DataFilel ) - Closes the file.

Write(DataFilel,MyData) - Writes a record to the current position of the file.

Read(DataFilel,MyData) -  Read the current record from the file.

Seek(DataFile1,5) - Move the file pointer to the 5th record on the file.

EOF(DataFilel ) - Returns True if you have reached the end of the file.

Writeln and Readln also can be used in reading and writing into text files. Writeln writes a newline characters to make the file more readable when displayed.

Read 5 Student's Details and Write them into a new binary File

PROGRAM Example13;
TYPE
StudType = RECORD
ld . integer;
Name . String[2O];
END;
VAR
File'l : FILE of StudType;
StudTable : ARRAYI1..5] of StudType; {Create an array of 5 records}
X . integer; iCounter variable)

BEIGN
{Get details from the user to enter into the file}
FOR X:='1 TO 5 Do
BEIGN
Writeln('Student -', X,'--------------');
Write('Enter lD: ');
Readln( StudTable[X]. ld);
Write('Name:');
Readln( StudTable[X]. Name);
Writeln;
END;

Assign( File1,'C:\temp\Students.dat');
Rewrite( Filel);

FOR X:=1 TO 5 DO
Write( File'l, StudTable[X] ); { Write 5 Records into the File }

Close( Filel );


This program copy one text file to another. Both file names are entered through keyboard.
PROGRAM Example14;
VAR
lnFile, OutFile :Text;
Filenamel, Filename2 : String[20];
Ch : Char

BEIGN
Write('Enter lnput File Name');
Readln ( Filenamel );

Write('Enter Output File Name');
Readln( FileName2 );
Assign( InFile, Filenamel );
Assign( OutFile, Filename2 );

Reset( lnFile );
Rewrite( OutFile );
IF lnFile=Nil OR OutFile=NIL THEN
Writeln('File Open Error')
ELSE
BEGIN
WHILE NOT EOF( InFile )DO
BEGIN
Read( lnFile, Ch );
Write( OutFile, Ch );
END;
Writeln(' File Successfully Copied ')l
END;
Close( InFile );
Close( OutFile );
END


Lecture 11 - Pascal Programming (Part V)



Array Processing
Array is a complex data structure which consist of number of elements of the same data type. These elements are located in consecutive bytes in the random access memory. Since we have a common name to access all elements of the array the position of the element should be given to access the array element. This is called the index or subscript. These arrays can be single dimensional or multi-dimensional.

Single Dimensional Arrays

VAR
Table : ARRAY[1..5] of integer;



The above array consist of 5 integer elements and the subscript of the elements are integer numbers from 1 to 5.

Table[2] := 10; { Store value 10 on 2nd element }
Table[4] := Table[2] + 5; { Store value 15 on 4th element }

TYPE
Table Type : ARRAY['B'..'E'] of real;
VAR
Num: TableType;



Above array is somewhat unusual and it shown the flexibility of array index in Pascal language. The array index can be :

  • Array index can be in any range, need not always start from 1.
  • Even characters can be used because it is also an ordinal type.
Following program uses an integer array of 5 elements and display the integers entered from the keyboard in reverse order of entry.

PROGRAM Example9;
VAR
Table . Array[1..Max] of integer;
Index : integer;

BEGIN
Writeln('Enter Integers separated by spaces');
FOR index:= 1 to Max DO
Read( Table[index] );

Writeln('The integers in reverse order are :'),
FOR index:= Max DOWNTO 1 DO
Write( Table[index]:4 );
Writeln;
END.


Two-dimensional Arrays
In Pascal we can define arrays which can spread in more than one direction. The most simplest of this is the two-dimensional array. Consider the following example which defines 2D char array which is used for a simple encryption process in the subsequent program.

VAR
Table : Array[1 ..4,1..5] of char,



Following program uses the shown 2D char array to input a string of 20 characters and encrypt it with simple transformation.

PROGRAM Examplell;
VAR
Table : Array[1 ..4,1..51of integer;
Rindex, Cindex : integer;

BEGIN
Writeln('Enter a sentence up to 20 character : ');
FOR Rindex:= 1 TO 4 DO
FOR Cindex:= 1 TO 5 DO
Read( Table[Rindex,Cindex] );

Write('Encrypted string is : ')
FOR Cindex:= 1 TO 5 DO
FOR Rindex.= 1 TO 4 DO
Write( Table[Rindex,Cindex] );
Writeln;

Write('Plaintext string is : ')
FOR Rindex:= 1 TO 4 DO
FOR Cindex:= 1 TO 5 DO
Write( Table[Rindex,Cindex] );
Writeln;
END.


Records

This is the other complex type which can consist of many fields. But unlike arrays all fields are not of the same type and therefore record can be defined as given below.

TYPE
StudentRecord = RECORD
ld : integer;
Name: String;
Avg : Real;
END;
VAR
MyData : StudentRecord;

You can have nested records by defining any field of the record also as another record type. For example DOB can be defined as DateType which consist of the fields DD, MM and YYYY


Following program read Student ID, Name and Avg into a student record and display them on the screen.

PROGRAM Examplel2;
TYPE
StudentRecord = RECORD
ld : integer;
Name: String;
Avg : Real;
END;
VAR
MyData : StudentRecord;

BEGIN
Write('Enter Student ld . ');
Readln( MyData.td );
Write('Enter Student Name : ,);
Readln( MyData.Na*"' );
Write('Enter Student Avg ; ,);
Readln( MyData.Avg');

Writeln('ld -',MyData.td );
Writeln('Name -',MyData.Name );
Writeln('Avg -', MyData.Avg );
END.


Important Notice!

Dear students and friends. When you commenting please do not mention your email address. Because your email address will be publicly available and visible to all. Soon, it will start sending tons of spams because email crawlers can extract your email from feed text.

To contact me directly regarding any inquiry you may send an email to info@bcslectures.website and I will reply accordingly.