Fundamentals of Programming: File handling

From Wikibooks, open books for an open world
Jump to: navigation, search

UNIT 1 - ⇑ Fundamentals of Programming ⇑

← User-defined data types File handling Validation →

Donkey Kong wiped your top score when you turned it off!

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[edit]

A common function needed in programs is to load data: saved games, text documents, spreadsheets. 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:

  Blank.svg Code Output
Down arrow Hexagonal Icon.svg

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[edit]

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

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()
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[edit]

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:

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
POBOX Company Description
223 Computering Co. badly spelt, slight smell.
657 Acme Deals in everything.

Answer :

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

File of records[edit]

Read/write records from/to a file

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.