Pascal Files

Files
In pascal, file organization falls into two categories. These are:
 * 1) Sequential file organization.
 * 2) Random file organization.

Sequential organization
The records have to be entered in a proper predefined sequence. The records will then be processed in the same order (sequence) they were entered first in first out (fifo).

It is not possible to process the intermediate records, without disturbing the neighboring records. This methord is better used where all the records need to be processed.

An example is the magnetic tape and cassette recorder.

Random organization
Records are entered in any manner annd you can process any record (makes it possible to process intermediary records without disturbing other records).

Example is the record player.

Pascal further provides basically two types of disc files:
 * 1) Binary files.
 * 2) Text files.

Typed Binary file
In order to use typed binary files, you first need to declare a file variable (this is done the same way as you would declare any variable).

Syntax: var variablename  :    file of datatype; Variablename is the name of the typed binary file (internal reference to the file). The datatype may be an integer, real or even a record type. After defining the file variable, we can move on to open the file.

Assign
This is a built in procedue that is used to open pascal files.

Syntax: assign (filevariable, 'filename.extension'); filevariable is also called internal reference to the file which should have been declared in the variable declaration section as we previously declared. filename is also called an external reference or directory reference to the file. A file may or may not have an extension name. When a file is opened, the basic operations to be performed are:
 * 1) Adding information to the file.
 * 2) Reading information from the file.
 * 3) Appending information in a file.

Adding information to a file
There are two built in procedures that can be used to add information to a file. 1. rewrite: It is used to write information in fresh files. If this procedure is used with an already existing file, the rewrite procedure will flush out the contents of the existing file and make the file a fresh one.

Syntax: rewrite (filevariable); 2. write: It is used to write records or data items into a file.

Syntax: write (filevariable, recordname); filevariable is the variable to which the file can write data.

Reading information from a file
There are two procedures that are used in reading information from a file, these are: 1. reset: It is used to read information from an existing file.

Syntax: reset (filevariable); If the procedure is used without an external reference in the  assign procedure , reset will generate an error message during run time. 2. read: It is used to read data items or a record from an existing file.

Syntax: read (filevariable, recordname); filevariable is a variable from which the file can read data. Instead of reading data itemes one by one, it is possible to read the whole record directly just by providing its record name.

Sample program: program sample (input, output); type studrec =  record stdname  :   string; stdexamno  :   integer; maths  :   integer; physics  :   integer; chem  :   integer; end; var recmark : studrec; infile  :   file of studrec; next :   char; begin assign (infile, 'scores.dat'); rewrite (infile); next  :=  'Y' or 'y'; while ((next  =  'Y') or (next = 'y'))  do                         begin with   recmark    do                                         begin write('Enter the Student Name  :  '); readln(stdname); write('Enter the Exam No  :  '); readln(stdexamno); write('Enter marks for Math : '); readln(maths); write('Enter marks for Physics : '); readln(physics); write('Enter marks for Chemistry : '); readln(chem); writeln(infile, recmark); end; writeln; writeln('--'); write ('Would you like to proceed (Y/N) '); readln(next); end; close(infile); end.

Appending information to a file
To add information to an existing file, we may use the append procedure. However, this procedure is only applicable to text files. In some cases we need to create our own code (routine) for appending information to files. The following routine is used to move the record pointer to the end of the file (so that writing can start from there onwards).

Syntax: while   not    eof(filevariable)    do         read (filevariable,  recordname); Example: while   not    eof(infile)    do         read (infile,  markrec); After the above code, the usual procedure code starts to add records from the end of the file as long as it is desired.

Example code: program appendsample (input, output); type studrec =  record stdname  :   string; stdexamno  :   integer; maths  :   integer; physics  :   integer; chem  :   integer; end; var recmark : studrec; infile  :   file of studrec; next :   char; begin assign (infile, 'scores.dat'); reset (infile); while  not   eof (infile)   do                        read (infile,  recmark); next  :=  'Y' or 'y'; while ((next  =  'Y') or (next = 'y'))  do                         begin with   recmark    do                                         begin write('Enter the Student Name  :  '); readln(stdname); write('Enter the Exam No  :  '); readln(stdexamno); write('Enter marks for Math : '); readln(maths); write('Enter marks for Physics : '); readln(physics); write('Enter marks for Chemistry : '); readln(chem); writeln(infile, recmark); end; write ('Do you want to proceed (Y/N) '); readln(next); end; close(infile); end.

Closing a file
All files used for processing should be closed after completion of the action being performed on it. Failure to close a file may be catastrophic in case you want to perform other operations on it later. The close bult in function is used to close files.

Syntax: close (filevariable);

Text File
Is used to mainly handle the manipulation of text (counting vowels or replace a character in a file). The file variable is declared as text.

Example: var texfile   :  text; The other key words for manipulating a file remain the same. A file is continuous if the write procedure is used and is line by line if writeln is used.

Sample Program: program sample  (input, output); var txtfile  :   text; conton   :   char; strMsg   :   string; begin assign (txtfile, 'learners.doc'); rewrite (txtfile); conton := 'Y'; while  (conton = 'Y')  do                    begin writeln ('Enter Message'); readln(strMsg); writeln(txtfile, strMsg); writeln; writeln ('Wnt to continue? (Y/N) '); readln(conton); writeln; end; close(txtfile); end.

Counting the number of letters
The function length, is used to count the number of letters in a line / string. This function can be employed in performing manipulations on text e.g, deleting, counting and adding. To come up with the best results, length is used in a loop. the for loop is the most used.

Example: program sampleer  (input, output); var txtfile  :   text; strMsg   :   string; i, ma, me, mi, mo, mu ; Integer; begin assign (txtfile, 'learners.doc'); reset (txtfile); ma := 0; me := 0; mi := 0; mo :=0; mu := 0; while not  eof (txtfile)  do                    begin readln(txtfile, strMsg); for i :=  1   to  Length(strMsg)  do                                   begin if  (strMsg(i) =  'a')  then  inc(ma); if  (strMsg(i) =  'e')  then  inc(me); if  (strMsg(i) =  'i')  then  inc(mi); if  (strMsg(i) =  'o')  then  inc(mo); if  (strMsg(i) =  'u')  then  inc(mu); end; end; writeln ('Number of  A ''s  ', Ma); writeln ('Number of  E ''s  ', Me); writeln ('Number of  I ''s  ', Mi); writeln ('Number of  O ''s  ', Mo); writeln ('Number of  U ''s  ', Mu); close (txtfile); end.

Random Files
In some instances we may want to process a record without disturbing the other records in a file. In such a case, random files are found to be most useful. The procedures for; opening, closing files, reading and writing a records are the same as sequential access. But in this case before entering a record, we must specify its record pointer (the pointer is an integer). After entering the record pointer, then the information relevant to that record is fed. Similarly when processing the data, the record pointer for the record which we want to to process is first entered, then the corresponding record for that record pointer is read and then processed.

Reading and writing information
The standard procedure, seek, is used when reading or writing information to or from a file at any particular time.

Syntax: seek (filevariable, position); filevariable is the typed binary file's variable. position is an integer variable and also a record number.

Example: program samplerandom (input, output); type emplrec =  record emplno  :   integer; emplname :  string; jbtitle    :   string; end; var emplinfo  :   emplrec; emplfile  :   file of emprec; choice  :   char; begin assign (emplfile, 'employee.dat'); rewrite (empfile); choice := 'Y'; while (choice = 'Y') do                        begin with  empinfo   do                                   begin write('Enter employee Number'); readln(emplno); seek(emplfile, emplno); write ('Enter Job Title'); readln(jbtitle); write(emplfile, empinfo); end; writeln; write('Would you like to continue? (y/N)  :  '); readln(choice); writeln; end; close (emplfile); end; writeln(txtfile, strMsg); writeln; writeln ('Wnt to continue? (Y/N) '); readln(conton); writeln; end; close(txtfile); end.