# 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.
| firefox.exe | file.rtf |
| MZ NULETXNULNULNULEOTNULNULNUL��NUL NUL,NULNULNULNULNULNULNUL@NULNULNUL NULNULNULNULNULNULNULNULNULNULNULNUL NULNULNULNULNULNULNULNULSOHNULNUL SOUSºSONUL´ Í!¸SOHLÍ!This program cannot be run in DOS mode. | {\rtf1\ansi\ansicpg1252\uc0\stshfdbch0 \stshfloch0\stshfhich0\stshfbi0\deff0 \adeff0{\fonttbl{\f0\froman\fcharset0 \fprq2{\*\panose 02020603050405020304} Times New Roman;}{\f1\froman\fcharset2 \fprq2{\*\panose 05050102010706020507} Symbol;} |
| As you can see the firefox.exe file is almost impossible to read in a text editor, but there are some recognisable strings in there | file.rtf is much clearer to read |
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.
* Example: Reading from a text file
* The following programs show how to open and read from a text (.txt) file
* VB.NET
* To do this you'll need the StreamReader, 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 myfile.txt and put it into a location you know the full address of, for example the root of the C:\ drive:
* 'you might need this so that you can use StreamReader
* Imports System.IO
* Module Module1
* Sub Main
* Dim filereader As StreamReader
* Dim filename As String
* Console.Write("name of file to load:")
* filename = Console.ReadLine 'this must be the full path of the file and the file extension
* filereader = New StreamReader(filename)
* Console.WriteLine("contents of file:")
* Console.WriteLine(filereader.ReadToEnd) 'write the whole file
* 'you can also use line = filereader.ReadLine to read a single line
* filereader.Close
* End Sub
* End Module
* The above code would produce the following:
* Code Output
* 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.
* Python 3
* To get this program to work you are going to need a file called myfile.txt and put it into a location you know the full address of, for example the root of the C:\ drive. Alternatively placing the text file in the same directory as your python program means you only need to specify the file name:
* filename = input("name of file to load: ")
* textFile = open(filename,"r")
* content = textFile.read
* textFile.close
* print("Contents of file:")
* print(content)
* The above code would produce the following:
* Code Output
* 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.
* Example: Writing to a text file
* 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!
* VB.NET
* This time we are using the StreamWriter:
* 'you might need this so that you can use StreamReader
* Imports System.IO
* Module Module1
* Sub Main
* Dim filewriter As StreamWriter
* Dim filename As String
* Dim texttofile As String
* filename = "C:/test1.txt" 'this is the location, file name and extension of what you are writing to.
* filewriter = New StreamWriter(filename)
* texttofile = Console.ReadLine
* filewriter.WriteLine(texttofile) 'write the line to the file
* filewriter.Close
* End Sub
* End Module
* Python 3
* filename = "C:/test1.txt" #this is the location, file name and extension of what you are writing to
* textToFile = input
* textFile = open(filename,"w") #opens the file for write "w" access
* textFile.write(textToFile) #writes the line to the file
* textFile.close
* Exercise: Reading and Writing Files
* 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
* Answer:
* Imports System.IO
* Dim filewriter As StreamWriter
* Dim filename As String
* Dim texttofile As String
* Dim line As String
* Console.Writeline("Please insert your shopping list:")
* While line <> ""
* line = Console.ReadLine
* If line <> "" Then
* texttofile = texttofile + line
* End If
* End While
* Console.Writeline("Where would you like to save it (give full location):")
* filename = Console.Readline
* filewriter = New StreamWriter(filename)
* filewriter.WriteLine(texttofile) 'write the line to the file
* filewriter.Close
* Python 3 answer
* Answer:
* textToFile = ""
* line = input("Please insert your shopping list:\n")
* while line != "":
* textToFile = textToFile + line + "\n"
* line = input
* filename = input("Where would you like to save it (give full location):\n")
* textFile = open(filename,"w")
* textFile.write(textToFile)
* textFile.close
* What does the extension of a file change about a file?
* Answer:
* 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
This would produce:
| ID | name | DoB | Comment |
| 1 | Peter | 12/12/82 | Nice person |
| 2 | Simon | 13/09/73 | Must try harder, or he will fail |
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.
Exercise: CSV
write a CSV file to hold the following table structure
POBOXCompanyDescription
223Computering Co.badly spelt, slight smell.
657AcmeDeals in everything.
Answer:
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.
* Example: Writing to a text file
* Creating a .csv file from a 2d array.
* Python 3
* This example uses ",".join(inner). 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.
* # mainArray contains the headers and 3 records for a database
* mainArray = [["Name","Age","Favourite Fruit"],
* ["Alice","23","Apple"],
* ["Bob","34","Banana"],
* ["Claire","45","Cherry"]]
* outputArray = #empty array. Each element will contain one line of csv data
* for inner in mainArray:
* csvLine = ",".join(inner) #each inner array (record) is turned into a single string
* csvLine = csvLine+"\n" #a newline character is added to the end
* outputArray.append(csvLine) #the finished string is appended to the output array
* outFile = open("C:\database.csv",mode="w")
* outFile.writelines(outputArray) #outputArray written in one go to C:\database.csv
* outFile.close
* Reading a .csv file into a 2d array
* Python 3
* 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.
* inFile = open("database.csv",mode="r")
* csvLines = inFile.readlines #csv file read into a 1d array
* inFile.close
* mainArray = #empty array defined
* for line in csvLines:
* line = line.strip #removes \n character from end of line
* inner = line.split(",") #line is split into an array "inner"
* mainArray.append(inner) #inner array appended into mainArray
Exercise: CSV File access
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
Answer:
looping = ""
outputArray =
while looping != "n":
forename = input("Forename :")
surname = input("Surname :")
email = input("Email :")
mobile = input("Mobile :")
csvLine = forename + "," + surname + "," + email + "," + mobile + "\n"
outputArray.append(csvLine)
looping = input("Hit enter to add another, enter n to stop")
op = open("friends.csv",mode="w")
op.writelines(outputArray)
op.close
Write a program that reads the .csv file from the question above into a 2d array.
Python 3 answer
Answer:
inFile = open("friends.csv",mode="r")
csvLines = inFile.readlines
inFile.close
friends =
for line in csvLines:
line = line.strip
innerArray = line.split(",")
friends.append(innerArray)
Extension: CSV Files
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. :
* Extension: XML
* 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 version="1.0"?>
* <school>
* <class name="UCOM2">
* <student>
* <name>Omer</name>
* <age>12</age>
* </student>
* <student>
* <name>Sandra</name>
* <age>12</age>
* </student>
* </class>
* </school>
* 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.