ALevelComputing_Session7

advertisement
Session Objectives#U2 S7
COULD explain different methods of organising files in programming terms.
SHOULD be able to estimate the size of a file
MUST understand the difference between an array and record
Code programmes which will read and write data to an external files in
Python.
A Level Computing#BristolMet
Key Words
A Level Computing#BristolMet
File Organisation
When programme files are created, the data used in that file is
stored in different ways.
Serial file – the data is stored in the order which it arrives.
New data is added to the end, which is called appending.
(e.g like a shopping list, would be a non computerised serial
file)
Sequential file – data is stored according to a key field in the
data that can be used to identify each individual record, like
an ID field. Adding new items requires the file to be recreated
as the new data needs to be inserted in the correct position
with the records and not simply added to the end. (e.g a class
register would be a non computerised sequential file)
Indexed Sequential – are sequential but have an index which
allows records to be found directly.
Random files – In all of the above examples, records are stored
next to each other. A random file allows the data to be stored
anywhere in a dedicated section of the disk. The location of the
field is decided by a calculation called a hash algorithm.
Finding these files is very quick, if you know the algorithm and
key field, therefore random files are useful for large
databases.
A Level Computing#BristolMet
File Handling Operations
It is often useful to programme data in an external file, this
requires the use of file operations to prepare, read and write
to file
Preparing a file – most languages use the OPEN command and
specify file name/path.
Read data from file – commands differ from among languages but
simplest way is to read one line at a time from a
serial/sequential files. For random files, lines are read from
specific address given in the code.
Writing data: This can merging (combining in new and old) as in
sequential files, or appending (adding to the end) as in serial
files.
A Level Computing#BristolMet
File Writing Operations
TASK: Use the writetofile program and experiment creating
different text files with different lines of text by changing
the commands.
TASK 2: Create an external text file, with a suitable filename,
containing the first 2 verses of a song or poem. NB Make sure
you have a line break between the 2 verses.
EXT: Extend your calculator programmes so that the answers are
written to an external file. (The file should contain some
explanatory text, ie The Answer = …
A Level Computing#BristolMet
Estimating File Sizes
It is possible to estimate the size of a file by calculating the
following:
• The size (in bytes) of each field in the record (for a string
use the largest size allowed)
• Add the field sizes to calculate the size of one record.
• Multiply the size of one record by the number of records in
the file
• Add 10% to the result, for additional storage that will be
needed to manage the file on disk (this is call an overhead).
TASK: Attempt Q2 on p.97 of the Hodder book.
A Level Computing#BristolMet
Download