Week11 2+12
Week11 2+12
Introduction to Computer
Programming
Violetta Cavalli-Sforza
Week 11, Lecture 2 (Wed)
THIS WEEK
• Today
– Programming with text files
– Quiz results & going over ..
• Thursday: LAB 01
– More work on homework #5
NEXT WEEK
• Quiz #4
– Wednesday May 30th
• No make-up session
• I don’t feel a need for more class time
but… I can possibly start classes 15
minutes early and end 15 minutes late
(since you have ½ hour breaks).
– Let me know if you want me to do that!
Topics
• Definitions
• Advantages of data files
• Text file operations
– Declare
– Initialize
– Use
– Close
• <eof> & <eoln> characters
• eof & eoln functions
• Reading/Writing characters
• Reading/Writing numeric data
Definitions
• Interactive program:
– Reads all input data from the keyboard and displays
output on the screen (terminal I/O)
• Batch processing:
– Input and output are from data files
• Pure batch processing:
– Input and output are strictly from data files, no
interaction with user
• Examples are the pipe and filter processing in Unix, Linux and
MSDOS
• Mixed interactive and batch processing:
– Have both terminal I/O and file I/O
Definitions [cont.]
• Data file (input file):
– A general definition
– A file containing input data for a program
– In Pascal, it could be a binary file (not readable by a
human) or a text file.
• Output file:
– A file containing program results
• Text file:
– A disk file containing a collection of characters
– Text files can be viewed or created using a text editor
– Can be used for input or output
Advantages of Using Data Files
• Entering data interactively
– Is ok, if you only have a few data items
– If you have lots of data, you want to do it ONCE and
then store the data
– It is easy to make errors
• If you do, it’s hard to fix them
• You need to start all over again … lots of extra work
• Entering data with a data file
– We can run the program as many times a you wish
without reentering the data.
• If program output is written to a file, a permanent
copy will be available.
• The output file generated by one program can
be used as input data to another file.
Advantages of Using Text Files
• Created with a text editor or similar tool
that can write text files
• Easy to check & edit.
• You can establish your own format, e.g.
– Have several fields per line
– Fields are separated by tab characters,
blanks, comma, or other special character
A File, Conceptually Is
• Example:
assign(InFile, 'InData.Dat' );
reset (InFile);
Opening/Preparing Input Files
[cont.]
• reset:
– Moves the file position pointer to the beginning of the file
– The file position pointer points to the file buffer and selects the
next character to be processed in the file
– Must be done before any characters are read from the input file
– An error is generated if the input file was not previously saved on
disk
• Example:
assign(OutFile, ‘OutData.dat' );
rewrite (OutFile);
Opening/Preparing Output Files
[cont.]
• rewrite:
– Prepares the file for output
– If the file doesn’t exist, an empty file is created
– If the file exists, file position pointer is put to the
beginning of the file.
– All old data are lost!
file
position
pointer
Closing Input & Output Files
• When you are done processing the file, close it:
close (input/output logical file name)
• Example:
VAR MyFile: text;
assign(MyFile, ‘MyData.txt' );
… {process data/file}
close(MyFile);
• Closing essentially makes sure the entire file is
saved correctly.
Self-Check 1
• What is wrong with this program?
assign(‘MyData.txt’, InFile);
rewrite(Infile); /* ?? */
reset(Infile);
WHILE NOT eoln DO /* ?? */
BEGIN
read(InFile, Next);
write(Next)
END; {while}
Example: Copying a file
PROGRAM CopyFile; {Copies InData file to OutData file}
VAR InData, {input file}
OutData : text; {output file}
PROCEDURE CopyLine (VAR InData, OutData : text);
{Copies a line of file InData to file OutData.
BEGIN . . . END { see next slide }
BEGIN {CopyFile}
assign (InData, ‘C:\TP\TEST\InFile.dat’);
assign (OutData, ‘C:\TP\TEST\OutFile.dat’);
reset (InData);
rewrite (OutData);
WHILE NOT eof(InData) DO
CopyLine (InData, OutData);
writeln (OutPut, 'Input file copied to output file.');
close (InData);
close (OutData)
end. {CopyFile}
Example: Copying a file [cont.]
PROCEDURE CopyLine (VAR InData, OutData : text);
{Copies a line of file InData to file OutData.}
VAR Ch : char;
BEGIN
END; {CopyLine}
Example: Copying a file
[alternative]
BEGIN {CopyFile}
END.
A Recursive Function that
Computes Powers
FUNCTION Exponentiate
(Num : real; Pow : integer) : real;
BEGIN
IF Pow = 0 THEN
Exponentiate := 1
ELSE
Exponentiate := Num *
Exponentiate(Num, Pow -1)
END; {Exponentiate}
A Recursive Function that
Computes Powers
FUNCTION Exponentiate
(Num : real; Pow : integer) : real;
BEGIN
IF Pow = 0 THEN
Exponentiate := 1
ELSE
Exponentiate := Num *
Exponentiate(Num, Pow -1)
END; {Exponentiate}
How Does it Work?
FUNCTION Exponentiate
(Num : real; Pow : integer) : real;
VAR Result : real;
BEGIN
IF Pow = 0 THEN
Result := 1
ELSE
Result := Num * Exponentiate(Num, Pow -1)
writeln('Num is ', Num:5:2, ' Pow is ', Pow:5,
' Result is ', Result:10:2);
Exponentiate := Result;
END; {Exponentiate}
Enter Number (real) and Power (integer): 3.00 4
Num is 3.00, Pow is 0, Result is 1.00
Num is 3.00, Pow is 1, Result is 3.00
Num is 3.00, Pow is 2, Result is 9.00
Num is 3.00, Pow is 3, Result is 27.00
Num is 3.00, Pow is 4, Result is 81.00
Why Recursion?
• Some problems lend themselves recursion
• Mathematically very elegant
• Computationally:
– More expensive
– But can be efficient in special cases
THE END!