File Handling
Advanced Higher Programming
What is a file?
 Up until now, any stored data within a
program is lost when the program
closes.
 A file is a permanent way to store data
File Handling
Three types of file can be used for
storing data
 SequentialSequential
 RandomRandom
 BinaryBinary
Sequential Files
Sequential files are useful for:
 Storing text
 Easy implementation in programs
 Where real-time editing of file(s) is not
required
Random Files
Random file structures are useful for
 Files that require real-time editing
 Storing records
Binary Files
Binary Files are useful for
 Storing numbers, programs and images
 Where no defined file structure is
present
 They will not be used in this course
Sequential Files
 Have a universal standard format and
are used in text editors such as
windows notepad
 Numerical data is stored as a string
e.g., 5.32 would be stored as “5.32”
 They are read from start to finish and so
cannot be read and written to
simultaneously
Sequential Files
 Data is ALWAYS written and retrieved
as CHARACTERS.
 Hence, any number written in this
mode will result in the ASCII Value of
the
number being stored.
 For Example, The Number 17 is stored
as two separate characters "1" and
"7".
Which means that 17 is stored as [ 49
55 ] and not as [ 17 ].
Sequential Files
 Are like a one dimensional array
 The text, ONE DAT might be stored as:
“ D TENO CR”A EO
F
Sequential Files
Files are manipulated in 3 stages:
 File Open
 Process File
 Close File
Sequential Files
File OpenFile Open
 If the file does not exist it is createdcreated
and then openedand then opened by the operating
system.
 A portion of memory (RAM)memory (RAM) is
reserved by the Operating System.
Sequential Files
Processing a FileProcessing a File
 When a file is open it can be written towritten to
or reador read from. (both in the case of
random and binary files)
 Writing to a file will save it to backingbacking
store.store.
Sequential FilesSequential Files
Closing a fileClosing a file
 When a file has been opened and
processed it must then be closedmust then be closed.
 The Operating system will then
release the memory.release the memory.
Visual Basic
 VB supports all three file types, but you
are only likely to use two of them
 Text files
 Random Access files
Text Files
Sequential/Text Files
Input File opened for read-only access.
Output File opened for output which is only write-to
or create
Append The file is opened for adding new data to an
existing file. This is the default setting.
Random The file is open for random access. This is
writing or reading one record at a time.
Binary The file is opened in binary mode
Using the OpenFileDialog control
Dim Filename as String
OpenFileDialog1.ShowDialog()
Filename= OpenFileDialog1.Filename
lblFilename.Text = Filename
Opening FilesOpening Files
FileOpen(1, Filename, OpenMode.Input) ‘to read from the file
FileOpen(1, Filename, OpenMode.Output) ‘to write to the file
FileOpen(1, Filename, OpenMode.Append) ‘to write to the end of
the file
Note – 1 assigns the file the number 1. All files are
identified by a number, not by their name. If you have two
or more files open at once they must have different
numbers.
Opening Files
 The FileOpen statement opens a file if it
exists. When you open a file to read
from it, an error results if it does not
exists. When you open a file to write to
it, if it doesn’t exist FileOpen first
creates it and opens it.
 Filename contains the name and path
of the file
Opening (creating) a Sequential File
This algorithm would achieve this:
1.1. Enter FilenameEnter Filename
2.2. Open File for writingOpen File for writing
3.3. Input InformationInput Information
4.4. Save to fileSave to file
5.5. Close fileClose file
Opening (creating) a Sequential File
This algorithm would achieve this:
 Enter FilenameEnter Filename
 Open File for writingOpen File for writing
 Input InformationInput Information
 Save to fileSave to file
 Close fileClose file
Filename= OpenFileDialog1.FileName
Opening (creating) a Sequential File
This algorithm would achieve this:
 Enter FilenameEnter Filename
 Open File for writingOpen File for writing
 Input InformationInput Information
 Save to fileSave to file
 Close fileClose file
FileOpen(1, Filename, OpenMode.Output)
Writeline(1, DataToBeWritten)
Opening (creating) a Sequential File
This algorithm would achieve this:
 Enter FilenameEnter Filename
 Open File for writingOpen File for writing
 Input InformationInput Information
 Save to fileSave to file
 Close fileClose file
Opening (creating) a Sequential File
This algorithm would achieve this:
 Enter FilenameEnter Filename
 Open File for writingOpen File for writing
 Input InformationInput Information
 Save to fileSave to file
 Close fileClose file
FileClose (1)
Opening a sequential fileOpening a sequential file
The final code would look like:
Dim Filename as string
‘File manipulation Program
‘Create File
Private sub cmdCreateFile_Click()
OpenFileDialog1.ShowDialog()
Filename = FileDialog1.FileName
FileOpen(1,Filename, OpenMode.Output)
WriteLine(1,DataToBeWritten)
FileClose (1)
End Sub

File handling

  • 1.
  • 2.
    What is afile?  Up until now, any stored data within a program is lost when the program closes.  A file is a permanent way to store data
  • 3.
    File Handling Three typesof file can be used for storing data  SequentialSequential  RandomRandom  BinaryBinary
  • 4.
    Sequential Files Sequential filesare useful for:  Storing text  Easy implementation in programs  Where real-time editing of file(s) is not required
  • 5.
    Random Files Random filestructures are useful for  Files that require real-time editing  Storing records
  • 6.
    Binary Files Binary Filesare useful for  Storing numbers, programs and images  Where no defined file structure is present  They will not be used in this course
  • 7.
    Sequential Files  Havea universal standard format and are used in text editors such as windows notepad  Numerical data is stored as a string e.g., 5.32 would be stored as “5.32”  They are read from start to finish and so cannot be read and written to simultaneously
  • 8.
    Sequential Files  Datais ALWAYS written and retrieved as CHARACTERS.  Hence, any number written in this mode will result in the ASCII Value of the number being stored.  For Example, The Number 17 is stored as two separate characters "1" and "7". Which means that 17 is stored as [ 49 55 ] and not as [ 17 ].
  • 9.
    Sequential Files  Arelike a one dimensional array  The text, ONE DAT might be stored as: “ D TENO CR”A EO F
  • 10.
    Sequential Files Files aremanipulated in 3 stages:  File Open  Process File  Close File
  • 11.
    Sequential Files File OpenFileOpen  If the file does not exist it is createdcreated and then openedand then opened by the operating system.  A portion of memory (RAM)memory (RAM) is reserved by the Operating System.
  • 12.
    Sequential Files Processing aFileProcessing a File  When a file is open it can be written towritten to or reador read from. (both in the case of random and binary files)  Writing to a file will save it to backingbacking store.store.
  • 13.
    Sequential FilesSequential Files Closinga fileClosing a file  When a file has been opened and processed it must then be closedmust then be closed.  The Operating system will then release the memory.release the memory.
  • 14.
    Visual Basic  VBsupports all three file types, but you are only likely to use two of them  Text files  Random Access files
  • 15.
  • 16.
    Sequential/Text Files Input Fileopened for read-only access. Output File opened for output which is only write-to or create Append The file is opened for adding new data to an existing file. This is the default setting. Random The file is open for random access. This is writing or reading one record at a time. Binary The file is opened in binary mode
  • 17.
    Using the OpenFileDialogcontrol Dim Filename as String OpenFileDialog1.ShowDialog() Filename= OpenFileDialog1.Filename lblFilename.Text = Filename
  • 18.
    Opening FilesOpening Files FileOpen(1,Filename, OpenMode.Input) ‘to read from the file FileOpen(1, Filename, OpenMode.Output) ‘to write to the file FileOpen(1, Filename, OpenMode.Append) ‘to write to the end of the file Note – 1 assigns the file the number 1. All files are identified by a number, not by their name. If you have two or more files open at once they must have different numbers.
  • 19.
    Opening Files  TheFileOpen statement opens a file if it exists. When you open a file to read from it, an error results if it does not exists. When you open a file to write to it, if it doesn’t exist FileOpen first creates it and opens it.  Filename contains the name and path of the file
  • 20.
    Opening (creating) aSequential File This algorithm would achieve this: 1.1. Enter FilenameEnter Filename 2.2. Open File for writingOpen File for writing 3.3. Input InformationInput Information 4.4. Save to fileSave to file 5.5. Close fileClose file
  • 21.
    Opening (creating) aSequential File This algorithm would achieve this:  Enter FilenameEnter Filename  Open File for writingOpen File for writing  Input InformationInput Information  Save to fileSave to file  Close fileClose file Filename= OpenFileDialog1.FileName
  • 22.
    Opening (creating) aSequential File This algorithm would achieve this:  Enter FilenameEnter Filename  Open File for writingOpen File for writing  Input InformationInput Information  Save to fileSave to file  Close fileClose file FileOpen(1, Filename, OpenMode.Output)
  • 23.
    Writeline(1, DataToBeWritten) Opening (creating)a Sequential File This algorithm would achieve this:  Enter FilenameEnter Filename  Open File for writingOpen File for writing  Input InformationInput Information  Save to fileSave to file  Close fileClose file
  • 24.
    Opening (creating) aSequential File This algorithm would achieve this:  Enter FilenameEnter Filename  Open File for writingOpen File for writing  Input InformationInput Information  Save to fileSave to file  Close fileClose file FileClose (1)
  • 25.
    Opening a sequentialfileOpening a sequential file The final code would look like: Dim Filename as string ‘File manipulation Program ‘Create File Private sub cmdCreateFile_Click() OpenFileDialog1.ShowDialog() Filename = FileDialog1.FileName FileOpen(1,Filename, OpenMode.Output) WriteLine(1,DataToBeWritten) FileClose (1) End Sub