You are on page 1of 21

File Processing

Chapter 9
Lesson Outline
Students to be able…
 To define and differentiate terms such as data file, text file
and output file.
 To know the advantages of using data files in programming
 To understand text file operations
 Declare
 Initialize
 Use
 Close
 Files and its related procedures. Ie. Reset, Assign,
Rewrite, Read, Write,Append etc.
 To write program using data files
Introduction
 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
 Mixed interactive and batch processing:
 Have both terminal I/O and file I/O
Definitions
 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 (read from) or output (write to)
Advantages of Using Data Files
 Entering data interactively
 Is ok, if you only have a few data items & do not need it anymore
 If you have number of data, you want to do it ONCE and then store
the data
 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
Text Files In General
 A collection of characters (sequentially read) stored under specific
name in secondary memory.
 Each character occupies one byte, including the <eoln> & <eof>
 Have no fixed size
 <eof> marks an end-of-file
 The last <eoln> character is followed by the <eof> character in the
file
Special File Characters
 <eoln>
 Indicates the end-of-line
 Inserted each time the keyboard's Enter key is pressed
 Similar to end-of-line in terminal input stream
 <eof>
 Indicates the end-of-file
 Inserted automatically after the last character of a text file when
the file is saved
A File, Conceptually Is
dataitem separator dataitem … dataitem separator dataitem <eoln>
dataitem separator dataitem … dataitem separator dataitem <eoln>
dataitem separator dataitem … dataitem separator dataitem <eoln>
..
..
dataitem separator dataitem … dataitem separator dataitem <eoln>
dataitem separator dataitem … dataitem separator dataitem <eoln>
<eof>
Writing Text Files
 Use write, and writeln
 Works as you are writing to standard output
 Can use as formatting directives
Using Text Files
 To use Text files in your program, you must:
1. Declare a text file variable
2. Associate the variable with a physical disk file
3. Open the file for reading or writing
4. Read and write to the file
5. Close the file
Declaring Text Files
 Pascal's predefined data type text is used to declare text
files.
 Syntax:
VAR file-name : text;
 Example:
VAR InData,OutData : text;
Text File Variables, Logical Files, and
Physical Files
 A file variable represents a logical file
 A logical file is explicitly associated with a physical file by stating
the drive, the path, and the physical file name
 The same file variable (logical file) can be associated with different
physical files in the same program but not at the same time.
 If you have to keep accessing several different input and/or output files at
the same time, use different file variables
Opening/Preparing Input Files: assign,
reset
 First, associate a file variable (logical file) with a physical file:
(Provide directory name)
assign(logical_file_name, physical_file_name);

 Then, prepare the file for reading:


(To Open the file)
reset ( input file logical name );

 Example:

VAR InFile: text;

assign(InFile, 'InData.txt' );
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

dataitem dataitem … dataitem <eoln>


dataitem dataitem … dataitem <eoln>
file
position dataitem dataitem … dataitem <eoln>
pointer …
<eof>
Opening/Preparing Output Files:
assign, rewrite
 First, associate a file variable (logical file) with a physical file:

assign(logical_file_name, physical_file_name);

 Then, prepare the file for writing:

rewrite (output file logical name );

 Example:

VAR OutFile: text;

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.
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

WHILE NOT eoln(InData) DO


BEGIN
read(InData,Ch);
write(OutData,Ch);
END;
readln(InData);
writeln(OutData);

END; {CopyLine}
Thinking Time
 Write a program to read a list of price based on item code
number. The program displays the price on the screen.

You might also like