Sequential Data Files (Text Files) Readings
Sequential Data Files (Text Files) Readings
The data of the text file is read sequentially i.e., sequential file data are read from the
initial location up to end of the desired data. Sequential access devices, such as
magnetic tape and compact disc(CD) are used to store the Sequential Data File.
1.2. Understand the generic statements using the file above to perform
the following:
1.2.1. Opening the sequential access file: Every file that is used in the program must
be opened before it can be used. The opening statements were categorized as
follows:
a) open input (name of file): This statement is used when an input file is
opened. E.g. open input (Students.txt)
b) open outputCreate (name of file): This statement is used when an output
file is opened to create a new output file.
E.g. open outputCreate (Students.txt)
c) open outputAppend (name of file): This statement is used when an output
file is opened to append or add a new text lines to it.
E.g. open outputAppend (Students.txt)
1
1.2.2. Closing a sequential access file: A file should be closed as soon as all the text
has been read from it to prevent loss of information when using an input file. .
a) close (name of file). E.g. close (Students.txt)
1.2.3. Reading the information from the sequential access file: A sequential access
file is always read from the beginning of the file. Lines can be read from a file until
the end-of-file (eof) character is found, which indicates that the file doesn’t
contain any data that has not been read.
a) read (name of file).E.g. StudentData = read(Students.txt)
1.2.1. Writing the information sequential access file: A text lines can be added to an
existing file or a new output file can be created.
a) write (name of file).E.g. write (Students.txt).
1.3. Pseudocode example 1: lines of text are read from the Student file.
if exists(Students.txt) then
open input(Students.txt)
EmployeeData = read(Students.txt)
do until eof
display EmployeeData
EmployeeData = read(Students.txt)
loop
close(Students.txt)
else
display “The file does not exist.”
Endif
Pseudocode Interpretation
open input(Students.txt) Open the Data file
called students.
EmployeeData = read(Students.txt) Read the content of
do until eof the Data file
display EmployeeData called students
EmployeeData = read(Students.txt) from the beginning
loop until the end(by
means of [do loop])
close(Students.txt) Close the Data file
called students.
2
1.4. Pseudocode example - 2: Student data will be accepted from the keyboard
and written to a new text file.
if exists(Students.txt) then
open outputCreate(Students.txt)
display “Enter the first line of Student data, enter -1 to stop”
enter StudentData
do while StudentData <> “-1”
write(Students.txt) from StudentData
write new line
display“Enter the next line of Students data, enter -1 to stop”
enter StudentData
loop
close(Students.txt)
Endif
Pseudocode Interpretation
write(Students.txt) from StudentData Write to a new text
file.
Every line of student
============================THE END=========================