File handling

25
File Handling Advanced Higher Programming

Transcript of File handling

Page 1: File handling

File Handling

Advanced Higher Programming

Page 2: File handling

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

Page 3: File handling

File HandlingThree types of file can be used for storing data

SequentialSequential RandomRandom BinaryBinary

Page 4: File handling

Sequential FilesSequential files are useful for:

Storing text Easy implementation in programs Where real-time editing of file(s) is

not required

Page 5: File handling

Random FilesRandom file structures are useful for

Files that require real-time editing Storing records

Page 6: File handling

Binary FilesBinary Files are useful for

Storing numbers, programs and images

Where no defined file structure is present

They will not be used in this course

Page 7: File handling

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

Page 8: File handling

Sequential Files Data is ALWAYS written and retrieved

as CHARACTERS. Hence, any number written in this

mode will result in the ASCII Value of thenumber 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 ].

Page 9: File handling

Sequential Files Are like a one dimensional array

The text, ONE DAT might be stored as:

“ D TENO CR”A EOF

Page 10: File handling

Sequential FilesFiles are manipulated in 3 stages:

File Open Process File Close File

Page 11: File handling

Sequential FilesFile OpenFile Open

If the file does not exist it is created and then openedcreated and then opened by the operating system.

A portion of memory (RAM)memory (RAM) is reserved by the Operating System.

Page 12: File handling

Sequential FilesProcessing a FileProcessing a File

When a file is open it can be written to or readwritten to or read from. (both in the case of random and binary files)

Writing to a file will save it to backing store.backing store.

Page 13: File handling

Sequential FilesSequential FilesClosing a fileClosing a file

When a file has been opened and processed it must then be must then be closedclosed.

The Operating system will then release the memory.release the memory.

Page 14: File handling

Visual Basic VB supports all three file types, but

you are only likely to use two of them Text files Random Access files

Page 15: File handling

Text Files

Page 16: File handling

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

Page 17: File handling

Using the OpenFileDialog controlDim Filename as StringOpenFileDialog1.ShowDialog()Filename=

OpenFileDialog1.FilenamelblFilename.Text = Filename

Page 18: File handling

Opening FilesOpening FilesFileOpen(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 fileNote – 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.

Page 19: File handling

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

Page 20: File handling

Opening (creating) a Sequential FileThis algorithm would achieve this:1.1. Enter FilenameEnter Filename2.2. Open File for writingOpen File for writing3.3. Input InformationInput Information4.4. Save to fileSave to file5.5. Close fileClose file

Page 21: File handling

Opening (creating) a Sequential FileThis 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

Page 22: File handling

Opening (creating) a Sequential FileThis 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)

Page 23: File handling

Writeline(1, DataToBeWritten)

Opening (creating) a Sequential FileThis 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

Page 24: File handling

Opening (creating) a Sequential FileThis 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)

Page 25: File handling

Opening a sequential fileOpening a sequential fileThe final code would look like:Dim Filename as string‘File manipulation Program‘Create FilePrivate sub cmdCreateFile_Click()

OpenFileDialog1.ShowDialog()Filename = FileDialog1.FileNameFileOpen(1,Filename, OpenMode.Output)WriteLine(1,DataToBeWritten)FileClose (1)

End Sub