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 |
File extensions such as .txt/csv/xls/pptx/exe are only there to tell the operating system or program how to handle them. They don't define what's inside, you could create an .exe file and fill it with pictures of kittens if you really wanted, the only problem would be that the Operating system wouldn't know what to do with it. By using file handling you can invent your own file extensions: myfile.pete |
When using text files, you read or write one line of text at a time as a string
Reading files
editA 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 '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:
Python 3 To get this program to work you are going to need a file called 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: Name of file to load: C:/belloc.txt
|
Writing files
editAs 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 '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
editA 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:
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
Answer:
|
File of records
editWhen 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 # 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: 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. |