A-level Computing 2009/AQA/Problem Solving, Programming, Data Representation and Practical Exercise/Fundamentals of Programming/File handling



 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. 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.

Writing files
As well as reading files it's important that we can write to files, this time 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

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
Read/write records from/to a file

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.



