0% found this document useful (0 votes)
19 views4 pages

Sequential File Handling

The document provides examples of sequential file handling in programming, including reading items into an array from a CSV file and writing data to a file. It details the pseudocode and Visual Basic code for reading test marks and writing average marks to files. Additionally, it covers declaring a record structure for student data and reading from a text file into an array of records.

Uploaded by

mashac
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views4 pages

Sequential File Handling

The document provides examples of sequential file handling in programming, including reading items into an array from a CSV file and writing data to a file. It details the pseudocode and Visual Basic code for reading test marks and writing average marks to files. Additionally, it covers declaring a record structure for student data and reading from a text file into an array of records.

Uploaded by

mashac
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Sequential File Handling

EXAMPLE 1 – Reading items into an array


Reading in and storing 10 marks from a file located named Test Marks.csv and store the items in an array called marks().
PSEUDOCODE VISUAL BASIC
1. Open file Test Marks.csv 1. Filename = “Test Marks.csv”
2. For index = 0 to 9 2. Fileopen(1, filename, openmode.input)
3. Input mark(index) 3. For index = 0 to 9
4. Next a. Input(1, marks(index))
5. Close file 4. Next
5. Fileclose(1)

EXAMPLE 2 – Writing data out to file


Write out the average test mark to a file named average.csv.

PSEUDOCODE VISUAL BASIC

1. Open file average.csv 1. Filename = “Average.csv”


2. Send average to FILE 2. Fileopen(2, filename, fileopen.output)
3. Close file a. Writeline(2, average)
3. Fileclose(2)
Sequential File Handling
EXAMPLE 3 – Declaring record structure, array of records and reading items into an array of records
Data
Forename Surname Class Shape Measure Time Arithmetic
Jessica Smith 2.3 89 56 75 78
Philip McCafferty 2.6 54 65 52 42
Sophie Watson 2.5 99 97 95 88
Millie Jones 2.4 23 35 40 52
Alfie McPherson 2.6 95 67 54 58

(a) Declare a record structure to store the data in the table above.

PSEUDOCODE VISUAL BASIC


RECORD IS pupils STRUCTURE pupils
Dim forename as string Dim forename as string
Dim surname as string Dim surname as string
Dim class as string (real accepted also) Dim class as string (real accepted also)
Dim shape as integer Dim shape as integer
Dim measure as integer Dim measure as integer
Dim time as integer Dim time as integer
Dim arithmetic as integer Dim arithmetic as integer
END RECORD END STRUCTURE

(b) Declare an array of records to store date for 200 pupils

Dim results(200) as pupils [199 also accepted]


(c) The data file is named pupils.txt. Using a programming language of your choice, write code that will read the data in
from file and store in the array of records declared in part (b).

PSEUDOCODE VISUAL BASIC


1. Open file pupils.txt 1. Filename = “pupils.txt”
2. For index = 0 to 199 2. Fileopen(1, filename, openmode.input)
3. Input results(index).forename 3. For index = 0 to 199
4. Input results(index).surname 4. Input(1, results(index).forename)
5. Input results(index).class 5. Input(1, results(index).surname)
6. Input results(index).shape 6. Input(1, results(index).class)
7. Input results(index).measure 7. Input(1, results(index).shape)
8. Input results(index).time 8. Input(1, results(index).measure)
9. Input results(index).arithmetic 9. Input(1, results(index).time)
10. Next 10. Input(1, results(index).arithmetic)
11. Close file 11. Next
12. Fileclose(1)
EXAMPLE 2 – Writing data out to file
Write out the average test mark to a file named average.csv.

PSEUDOCODE
1. Open file average.csv
2. For index = 0 to 199
3. Average=results(index).shape+results(index).measure+results(index).time+ results(index).arithmetic
4. Send average to FILE
5. next
6. Close file

VISUAL BASIC
1. Filename = “Average.csv”
2. Fileopen(2, filename, fileopen.output)
3. For index = 0 to 199
a. Average=results(index).shape+results(index).measure+results(index).time+ results(index).arithmetic
b. Writeline(2, average)
4. next
5. Fileclose(2)

You might also like