Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSM18 FilesDr Terry Hinton1 VB - Persistence in software Information entered into a program is volatile Can switch computer off Power can be removed accidentally.

Similar presentations


Presentation on theme: "CSM18 FilesDr Terry Hinton1 VB - Persistence in software Information entered into a program is volatile Can switch computer off Power can be removed accidentally."— Presentation transcript:

1 CSM18 FilesDr Terry Hinton1 VB - Persistence in software Information entered into a program is volatile Can switch computer off Power can be removed accidentally Power-cuts can occur Computer System can crash We need to make data persist beyond the execution of a program

2 CSM18 FilesDr Terry Hinton2 Files  Files  In administration, a file is used to keep documents together in an orderly way  In a computer, a file is storage of information usually on magnetic media - discs, tapes  A computer file stores data in an order way  items are written to the file in strict sequence  items are read from the file in the same sequence

3 CSM18 FilesDr Terry Hinton3 Computer file mechanisms  In a computer, there must be facilities to:  Open a file, read items from a file, write items to a file and close files  to maintain a link between a program and an open file, the system assigns a file reference or handle; a variable by which the file can be identified  programming languages provide commands to access files - open, read, write,close

4 CSM18 FilesDr Terry Hinton4 Creating a file  To create a computer file  Ask the O/S for a file handle  Open the file for write access  Send data item variables to the file in some order  Close the file  The order of the file is defined at the point of creation  Care required to prevent file errors due to upset sequence

5 CSM18 FilesDr Terry Hinton5 Code to create a file Sub CreateFile( ) Dim File As Integer ‘ File handle Dim Name As String Dim Number As Integer File = FreeFile Open “MyFile.dat” For Output As #File Name = InputBox(“Name?”) Write #File, Name Number = InputBox(“Number?”) Write #File, Number Close #File End Sub Sub CreateFile( ) Dim File As Integer ‘ File handle Dim Name As String Dim Number As Integer File = FreeFile Open “MyFile.dat” For Output As #File Name = InputBox(“Name?”) Write #File, Name Number = InputBox(“Number?”) Write #File, Number Close #File End Sub Ask for handle Open File Send data Close file

6 CSM18 FilesDr Terry Hinton6 Retrieving data from a file  To retrieve data from a file  Ask the O/S for a file handle  Open the file for read access  Read data items from the file - they will be in the same order as when written  Close the file  File problems generally due to sequence errors  e.g. program reads a string when expecting a number

7 CSM18 FilesDr Terry Hinton7 Code to retrieve data from a file Sub ReadFile( ) Dim File As Integer ‘ File handle Dim Name As String Dim Number As Integer File = FreeFile Open “MyFile.dat” For Input As #File Input #File, Name MsgBox Name Input #File, Number MsgBox Number Close #File End Sub Sub ReadFile( ) Dim File As Integer ‘ File handle Dim Name As String Dim Number As Integer File = FreeFile Open “MyFile.dat” For Input As #File Input #File, Name MsgBox Name Input #File, Number MsgBox Number Close #File End Sub Ask for handle Open File Read data Close file

8 CSM18 FilesDr Terry Hinton8 Types of file  Files can be organised in a number of ways  Tabular  Character based text  Text with line-breaks  Sequential - write or read whole file  Random access - access an item using index  File organisation is determined by...  the program code that writes the file  the variable types stored

9 CSM18 FilesDr Terry Hinton9 File formats “Fred Bloggs”, “1 High Street”, £200 “Jane Smith”, “55 Glen Road”, £28.55 The key feature of this file is that it is composed of a continu ous sequence of text characters with no format beyond the la nguage used. Strictly, it should be seen as one long line of te xt characters. This type of file is distinguished by the use of line breaks (special, non-printing characters) to preserve the format of the text. Each line ends with a line-break and so need not be a fixed length. Tabular Text with Line breaks Character

10 CSM18 FilesDr Terry Hinton10 Files and objects We send objects to a file by serialising them The data in each object is written out as a sequence of values, either in binary or character form The class is responsible for sending and retrieving object data Call a serialised file of objects to be a stream Use streams to transmit and receive objects over a number of different media

11 CSM18 FilesDr Terry Hinton11 Streams Provide an object with Save and Load methods By keeping the code within the class, objects manage their own streams Complex hierarchical objects can manage themselves hierarchically

12 CSM18 FilesDr Terry Hinton12 Objects and Databases Streams of objects have an inherent problem Need to send and retrieve the whole hierarchy No chance of reading one or two items back OK for a few hundred or thousand items Not possible for millions In this case, a database provides the solution Structured random access Fast retrieval of objects and sets of objects

13 CSM18 FilesDr Terry Hinton13 From Array to File Dim Data (1..100) As Single For I= 1 to 100 Data(I) = … Next I For I= 1 to 100 Write #File, Data(I) Next I

14 CSM18 FilesDr Terry Hinton14 Visual Basic End


Download ppt "CSM18 FilesDr Terry Hinton1 VB - Persistence in software Information entered into a program is volatile Can switch computer off Power can be removed accidentally."

Similar presentations


Ads by Google