A-level Computing/AQA/Paper 1/Fundamentals of data structures/Fields records and files



 Early Arcade machines stored your high scores, but as soon as you turned the machine off then all the scores were wiped! Nowadays it is unthinkable to have a game where you don't save scores, trophies, achievements or your progress. Writing to files allows people to do this alongside all the other essential file writing such as saving text documents, music and images.

Another use of files is reading data into your program: think about when you load a saved game, or open a spreadsheet. Both of these things involve reading a file.

Files might have lots of strange file extensions like .doc .txt .html .css .odt. In fact there are thousands of them and they tell the Operating System what program to use to read the file. However, if you open each file with a text editor such as notepad++ you might be able to see underlying structure of the file. Some of them will be plain text and some of them gobbledygook. We are going to learn how to read and write data directly to these files.

When using text files, you read or write one line of text at a time as a string

Reading files
A common function needed in programs is to load data: saved games, text documents, spreadsheets.

The following programs show how to open and read from a text (.txt) file To do this you'll need the, a collection of functions used to read data from files into our program. To get this program to work you are going to need a file called  and put it into a location you know the full address of, for example the root of the   drive: The above code would produce the following:

name of file to load: C:/belloc.txt contents of file: To sleep and smell the incense of the tar, To wake and watch Italian dawns aglow And underneath the branch a single star, Good Lord, how little wealthy people know.

To get this program to work you are going to need a file called  and put it into a location you know the full address of, for example the root of the   drive. Alternatively placing the text file in the same directory as your python program means you only need to specify the file name:

The above code would produce the following:

Name of file to load: C:/belloc.txt Contents of file: To sleep and smell the incense of the tar, To wake and watch Italian dawns aglow And underneath the branch a single star, Good Lord, how little wealthy people know.

Writing files
As well as reading files it's important that we can write to files.

The following programs show how to open and write to a text (.txt) file. Note that doing this will overwrite existing files of the same name without warning!

This time we are using the :

write code to save a shopping list typed in by a user (they finish typing the list by entering a blank line) to a file called specified by the user

VB.NET answer

Python 3 answer

What does the extension of a file change about a file?

It only changes how a program or an Operating System handles the file, it doesn't change anything inside the file

CSV
A special sort of file is a Comma Separated Value (.csv) file. This file is used for store spreadsheet information with each comma denoting a new cell, and each new line a new row. For example in the code below there are three columns and three rows. If you need to include a comma in a cell you have to encase the cell text in speech marks ID, name, DoB, Comment 1, Peter, 12/12/12, Nice person 2, Simon, 13/13/13, "Must try harder, or he will fail"

This would produce: You might notice that .csv files are very simple and don't store any information on datatypes or style. So if you were to save your spreadsheet in Microsoft Excel as a .csv file you would lose all this information. In general .csv files are a small and simple way to transfer data.

write a CSV file to hold the following table structure


 * POBOX,Company,Description
 * 223,Computering Co.,"badly spelt, slight smell"
 * 657,Acme,Deals in everything.

File of records
When stored as (.csv), each line of a file is a data record. Each record consists of one or more fields, separated by commas. You should be able to move data between a 2d array and a csv file. The following code gives you an example of how to do this.

Creating a .csv file from a 2d array.

This example uses. Join is a method of a string in python. It takes an array of strings and joins them into a single string using (in this case) a comma as glue. Note that if your array contains any non-string data types, this will not work and you will need to convert the elements of your array into strings first.

Reading a .csv file into a 2d array

Note that readlines reads a text file into a one dimensional array of text. Each line from the original text file becomes a unique element in that array and includes the \n character from the end of each line.

Write a program that asks you for the names, email and mobile phone number of your friends then writes them into a csv file.

Python 3 answer

Write a program that reads the .csv file from the question above into a 2d array. Python 3 answer

Write a program which extends the tasks above allowing you to maintain a friends list. You could include features such as searching for, adding, editing and deleting friends.

A lot of modern programs and data formats such as LibreOffice and .odf use XML to store data, then compress the data to make the files smaller. XML is very similar to HMTL and SVG in that is uses tags, but the great thing about it is that you define what each tag is. Take a look at this example: XML has become increasingly important, especially with regards to the internet, if you want to learn more you can check out the tutorial at w3schools.

However there are people who hate XML: the files it makes are generally quite large, having to store all that tag information and when the internet is involved speed is often of the essence. A leaner way of sending data from one place to another is JSON which you can also find out about at w3schools.



