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


1 comment:

  1. You have elaborated pascal programming soo well. Simply loving it. I had a project and this blog is working wonders for me. Thank you so much for writing

    ReplyDelete

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.