File Handling




Learning Intentions and Success Criteria

Learning Intentions

Success Criteria

In this tutorial I am going to learn :
  • how to ...
By the end of the tutorial I will be able to :
  • ...


Overview


Sequential (text) files have a universal standard format and are used extensively in simple text editors.

Sequential files are read from beginning to end and so the files cannot be read and written to simultaneously.

A sequential data file can be thought of as a 1-dimensional array with each array location storing a single byte of data, such as an ASCII character.

The manipulation of files involves 3 stages:
Opening a file
If the file does not exist then it is created and then opened by the operating system, which reserves a portion of memory for the file.
Processing a file
Once a file is opened it can be written to or read from, or both in the case of random and binary files. Writing to a file will save it to backing store.
Closing a file
Once a file has been opened and processed it must then be closed. When a file is closed the operating system releases the memory that it reserved for the file.

The vb code for opening a file which will be written to is :

Open "c:\My Documents\trial.txt" For Output As #1

The vb code for opening a file which will be read from is :

Open "c:\My Documents\trial.txt" For Input As #1


Opening a Sequential File and writing to it


Opening (creating) a sequential file
Before data can be written to a sequential file, the file must be created. Characters are then 'printed' to the file using a simple text window and the file is then closed. The following algorithm will achieve this:


1.    Enter filename
2.    Open file for writing
3.    Input information
4.    Save to file
5.    Close file</span>

The corresponding Visual Basic code is:


Filename = InputBox$("Please enter a filename")
Open Filename For Output As #1
Print #1, Text1.Text
Close #1
End Sub

Opening a Sequential File and Reading from it


Reading a text file
Once a file has been created it can be read at any time. The following algorithm will achieve this:

1. Enter filename
2. Open file for reading using input
3. Close file

The corresponding Visual Basic code is shown in Code 5.2:


Filename = InputBox$("Please input a filename")
 Open Filename For Input As #1
 Text1.Text = Input$(LOF(1), 1)
 Close #1
 

Deleting a File


Deleting a file
Any file may be permanently deleted using the Kill statement within Visual Basic.
The following algorithm will achieve this:

1. Get file
2. Delete file? Yes/No
3. If 'yes' then
4. Kill file

The Visual Basic code for this is:

Dim Response As String
 Response = MsgBox("Are you sure you want to delete", vbYesNo)
 If Response = vbYes Then
 Kill Filename
End If

Video Conference


Embed Video Conference here

Additional Videos


Embed video here

Exam Questions


Embed Document here



Summary

  • Please make a list of essential key-points here


Files

  • upload files here



Links