Sequential Access Files - PowerPoint PPT Presentation

1 / 86
About This Presentation
Title:

Sequential Access Files

Description:

Can be typed to the screen. 2 Special Characters. Sequential Access Files ... Strings need to be delimited (enclosed) in quotation marks ... – PowerPoint PPT presentation

Number of Views:143
Avg rating:3.0/5.0
Slides: 87
Provided by: debrac7
Category:

less

Transcript and Presenter's Notes

Title: Sequential Access Files


1
Sequential Access Files
  • ITE 285

2
Sequential Access Files
  • Also known as __________ or __________
  • Data is stored as a __________
  • Efficient use of storage
  • Can be read / created with __________
  • Can be typed to the screen
  • 2 Special Characters
  • __________
  • __________

3
Sequential Access Files
  • Also known as text files or ASCII files
  • Data is stored as a __________
  • Efficient use of storage
  • Can be read / created with __________
  • Can be typed to the screen
  • 2 Special Characters
  • __________
  • __________

4
Sequential Access Files
  • Also known as text files or ASCII files
  • Data is stored as a sequence of characters
  • Efficient use of storage
  • Can be read / created with __________
  • Can be typed to the screen
  • 2 Special Characters
  • __________
  • __________

5
Sequential Access Files
  • Also known as text files or ASCII files
  • Data is stored as a sequence of characters
  • Efficient use of storage
  • Can be read / created with text editors
  • Can be typed to the screen
  • 2 Special Characters
  • __________
  • __________

6
Sequential Access Files
  • Also known as text files or ASCII files
  • Data is stored as a sequence of characters
  • Efficient use of storage
  • Can be read / created with text editors
  • Can be typed to the screen
  • 2 Special Characters
  • Carriage Return
  • Line Feed

7
Additional Requirements
  • __________
  • Strings need to be delimited (enclosed) in
    quotation marks
  • Will work without except special cases (including
    commas in text)
  • __________
  • Separate data elements (fields) with commas

8
Additional Requirements
  • String Delimiters
  • Strings need to be delimited (enclosed) in
    quotation marks
  • Will work without except special cases (including
    commas in text)
  • __________
  • Separate data elements (fields) with commas

9
Additional Requirements
  • String Delimiters
  • Strings need to be delimited (enclosed) in
    quotation marks
  • Will work without except special cases (including
    commas in text)
  • Comma Separators
  • Separate data elements (fields) with commas

10
Data Storage
  • Data is stored as a continuous stream of
    characters
  • Data access always from __________
  • Read through to __________
  • Data cannot be __________ into the middle of file
  • Data cannot be __________
  • Like a cassette tape

11
Data Storage
  • Data is stored as a continuous stream of
    characters
  • Data access always from beginning of the file
  • Read through to __________
  • Data cannot be __________ into the middle of file
  • Data cannot be __________
  • Like a cassette tape

12
Data Storage
  • Data is stored as a continuous stream of
    characters
  • Data access always from beginning of the file
  • Read through to end of the file
  • Data cannot be __________ into the middle of file
  • Data cannot be __________
  • Like a cassette tape

13
Data Storage
  • Data is stored as a continuous stream of
    characters
  • Data access always from beginning of the file
  • Read through to end of the file
  • Data cannot be inserted into the middle of file
  • Data cannot be __________
  • Like a cassette tape

14
Data Storage
  • Data is stored as a continuous stream of
    characters
  • Data access always from beginning of the file
  • Read through to end of the file
  • Data cannot be inserted into the middle of file
  • Data cannot be edited
  • Like a cassette tape

15
Data in File
  • Can be the __________ on every line
  • Same number of data elements
  • Same data type
  • Same order
  • Example
  • Al Anderson,10.00,45 notice NO spaces
  • Betty Baker,11.50,40
  • Cathy Carter,9.50,20

16
Data in File
  • Can be the same on every line
  • Same number of data elements
  • Same data type
  • Same order
  • Example
  • Al Anderson,10.00,45 notice NO spaces
  • Betty Baker,11.50,40
  • Cathy Carter,9.50,20

17
Data in File
  • Each line can also contain totally different data
  • Example
  • 3200 Main Street
  • Apt. B
  • Mobile,Al,36695

18
Operations with Sequential Files
  • __________(Modes Input, Output, Append)
  • __________ from the File
  • __________ to the File (also Append)
  • __________ the File
  • __________ Files
  • __________ (Data?, Files)
  • __________
  • __________
  • __________
  • __________

19
Operations with Sequential Files
  • Open File (Modes Input, Output, Append)
  • __________ from the File
  • __________ to the File (also Append)
  • __________ the File
  • __________ Files
  • __________ (Data?, Files)
  • __________
  • __________
  • __________
  • __________

20
Operations with Sequential Files
  • Open File (Modes Input, Output, Append)
  • Read Data from the File
  • __________ to the File (also Append)
  • __________ the File
  • __________ Files
  • __________ (Data?, Files)
  • __________
  • __________
  • __________
  • __________

21
Operations with Sequential Files
  • Open File (Modes Input, Output, Append)
  • Read Data from the File
  • Write Data to the File (also Append)
  • __________ the File
  • __________ Files
  • __________ (Data?, Files)
  • __________
  • __________
  • __________
  • __________

22
Operations with Sequential Files
  • Open File (Modes Input, Output, Append)
  • Read Data from the File
  • Write Data to the File (also Append)
  • Close the File
  • __________ Files
  • __________ (Data?, Files)
  • __________
  • __________
  • __________
  • __________

23
Operations with Sequential Files
  • Open File (Modes Input, Output, Append)
  • Read Data from the File
  • Write Data to the File (also Append)
  • Close the File
  • Rename Files
  • __________ (Data?, Files)
  • __________
  • __________
  • __________
  • __________

24
Operations with Sequential Files
  • Open File (Modes Input, Output, Append)
  • Read Data from the File
  • Write Data to the File (also Append)
  • Close the File
  • Rename Files
  • Deleting (Data?, Files)
  • __________
  • __________
  • __________
  • __________

25
Operations with Sequential Files
  • Open File (Modes Input, Output, Append)
  • Read Data from the File
  • Write Data to the File (also Append)
  • Close the File
  • Rename Files
  • Deleting (Data?, Files)
  • Error Checking
  • __________
  • __________
  • __________

26
Operations with Sequential Files
  • Open File (Modes Input, Output, Append)
  • Read Data from the File
  • Write Data to the File (also Append)
  • Close the File
  • Rename Files
  • Deleting (Data?, Files)
  • Error Checking
  • Searching
  • __________
  • __________

27
Operations with Sequential Files
  • Open File (Modes Input, Output, Append)
  • Read Data from the File
  • Write Data to the File (also Append)
  • Close the File
  • Rename Files
  • Deleting (Data?, Files)
  • Error Checking
  • Searching
  • Sorting???
  • __________

28
Operations with Sequential Files
  • Open File (Modes Input, Output, Append)
  • Read Data from the File
  • Write Data to the File (also Append)
  • Close the File
  • Rename Files
  • Deleting (Data?, Files)
  • Error Checking
  • Searching
  • Sorting???
  • Merging???

29
File Processing
  • 3 Main Steps
  • __________
  • __________
  • __________
  • System.IO file libraries
  • We must import System.IO to include file
    processing
  • Imports System.IO ? In General Declarations

30
File Processing
  • 3 Main Steps
  • Open the File
  • __________
  • __________
  • System.IO file libraries
  • We must import System.IO to include file
    processing
  • Imports System.IO ? In General Declarations

31
File Processing
  • 3 Main Steps
  • Open the File
  • Read from / Write To the File
  • __________
  • System.IO file libraries
  • We must import System.IO to include file
    processing
  • Imports System.IO ? In General Declarations

32
File Processing
  • 3 Main Steps
  • Open the File
  • Read from / Write To the File
  • Close the File
  • System.IO file libraries
  • We must import System.IO to include file
    processing
  • Imports System.IO ? In General Declarations

33
Opening a Sequential File
  • FileOpen (FileNumber, FileName, Mode)
  • __________ Reference number between 1 511
  • Each file has its own unique file number
  • __________ The name of the file
  • Must include path name if not in the default
    directory (bin\debug)
  • __________
  • OpenMode.Input
  • OpenMode.Output
  • OpenMode.Append
  • Can only be open in one mode at a time

34
Opening a Sequential File
  • FileOpen (FileNumber, FileName, Mode)
  • FileNumber Reference number between 1 511
  • Each file has its own unique file number
  • __________ The name of the file
  • Must include path name if not in the default
    directory (bin\debug)
  • __________
  • OpenMode.Input
  • OpenMode.Output
  • OpenMode.Append
  • Can only be open in one mode at a time

35
Opening a Sequential File
  • FileOpen (FileNumber, FileName, Mode)
  • FileNumber Reference number between 1 511
  • Each file has its own unique file number
  • FileName The name of the file
  • Must include path name if not in the default
    directory (bin\debug)
  • __________
  • OpenMode.Input
  • OpenMode.Output
  • OpenMode.Append
  • Can only be open in one mode at a time

36
Opening a Sequential File
  • FileOpen (FileNumber, FileName, Mode)
  • FileNumber Reference number between 1 511
  • Each file has its own unique file number
  • FileName The name of the file
  • Must include path name if not in the default
    directory (bin\debug)
  • Mode
  • OpenMode.Input
  • OpenMode.Output
  • OpenMode.Append
  • Can only be open in one mode at a time

37
OpenMode.Input
  • Read data from the file __________
  • File.Exists (filename) function
  • File Not Found runtime error
  • Function to determine if the file exists
  • Returns boolean
  • If file.exists(a\myfile) then
  • open the file
  • FileOpen(1, a\myfile, OpenMode.Input)
  • Else
  • MessageBox.Show (Invalid File)
  • End If

38
OpenMode.Input
  • Read data from the file into the program
  • File.Exists (filename) function
  • File Not Found runtime error
  • Function to determine if the file exists
  • Returns boolean
  • If file.exists(a\myfile) then
  • open the file
  • FileOpen(1, a\myfile, OpenMode.Input)
  • Else
  • MessageBox.Show (Invalid File)
  • End If

39
OpenFile.Output
  • Write data __________ to the file
  • Write data at the __________ of the file
  • Existing data will be overwritten
  • If file does not exist, it will create it

40
OpenFile.Output
  • Write data from the program to the file
  • Write data at the __________ of the file
  • Existing data will be overwritten
  • If file does not exist, it will create it

41
OpenFile.Output
  • Write data from the program to the file
  • Write data at the BEGINNING of the file
  • Existing data will be overwritten
  • If file does not exist, it will create it

42
OpenFile.Append
  • Write data __________ to the file
  • Will write to the __________ of an existing file
  • If the file does not exist, it will create it

43
OpenFile.Append
  • Write data from the program to the file
  • Will write to the __________ of an existing file
  • If the file does not exist, it will create it

44
OpenFile.Append
  • Write data from the program to the file
  • Will write to the END of an existing file
  • If the file does not exist, it will create it

45
Read Data from the File
  • File must be open for __________
  • Read __________ Input(fileNumber, Variable)
  • Variable must be of the same data type as the
    data in the file
  • Each data item must be read in (starting at
    beginning)
  • Can use EOF functions
  • Determine End of File
  • While NOT EOF (fileNumber)
  • process file
  • End While

46
Read Data from the File
  • File must be open for INPUT
  • Read __________ Input(fileNumber, Variable)
  • Variable must be of the same data type as the
    data in the file
  • Each data item must be read in (starting at
    beginning)
  • Can use EOF functions
  • Determine End of File
  • While NOT EOF (fileNumber)
  • process file
  • End While

47
Read Data from the File
  • File must be open for INPUT
  • Read One Item from the File Input(fileNumber,
    Variable)
  • Variable must be of the same data type as the
    data in the file
  • Each data item must be read in (starting at
    beginning)
  • Can use EOF functions
  • Determine End of File
  • While NOT EOF (fileNumber)
  • process file
  • End While

48
Read in Entire File
  • InputString (fileNumber, amountToReadIn)
  • Will read in a __________
  • LOF Length of File
  • InputString (fileNumber, LOF(fileNumber))

49
Read in Entire File
  • InputString (fileNumber, amountToReadIn)
  • Will read in a sequence of characters
  • LOF Length of File
  • InputString (fileNumber, LOF(fileNumber))

50
Write Data to the File
  • File must be Open for __________ or __________
  • __________
  • Write (fileNumber, data)
  • can use variable, data, multiple data items,
    etc.
  • __________
  • Print (fileNumber, data)
  • works like the write
  • __________
  • WriteLine (fileNumber, data)
  • works like the write, but insers carriage
    return line feed

51
Write Data to the File
  • File must be Open for Output or Append
  • __________
  • Write (fileNumber, data)
  • can use variable, data, multiple data items,
    etc.
  • __________
  • Print (fileNumber, data)
  • works like the write
  • __________
  • WriteLine (fileNumber, data)
  • works like the write, but insers carriage
    return line feed

52
Write Data to the File
  • File must be Open for Output or Append
  • Write procedure
  • Write (fileNumber, data)
  • can use variable, data, multiple data items,
    etc.
  • __________
  • Print (fileNumber, data)
  • works like the write
  • __________
  • WriteLine (fileNumber, data)
  • works like the write, but insers carriage
    return line feed

53
Write Data to the File
  • File must be Open for Output or Append
  • Write procedure
  • Write (fileNumber, data)
  • can use variable, data, multiple data items,
    etc.
  • Print procedure
  • Print (fileNumber, data)
  • works like the write
  • __________
  • WriteLine (fileNumber, data)
  • works like the write, but insers carriage
    return line feed

54
Write Data to the File
  • File must be Open for Output or Append
  • Write procedure
  • Write (fileNumber, data)
  • can use variable, data, multiple data items,
    etc.
  • Print procedure
  • Print (fileNumber, data)
  • works like the write
  • WriteLine procedure
  • WriteLine (fileNumber, data)
  • works like the write, but insers carriage
    return line feed

55
Close the File
  • FileClose (fileNumber)
  • Breaks __________ link with the file
  • Disassociated the __________ from the physical
    file
  • After closing file
  • You may reopen the file in another mode
  • Can reuse file number
  • You must close the file after all data has been
    read or written

56
Close the File
  • FileClose (fileNumber)
  • Breaks communication link with the file
  • Disassociated the __________ from the physical
    file
  • After closing file
  • You may reopen the file in another mode
  • Can reuse file number
  • You must close the file after all data has been
    read or written

57
Close the File
  • FileClose (fileNumber)
  • Breaks communication link with the file
  • Disassociated the file number from the physical
    file
  • After closing file
  • You may reopen the file in another mode
  • Can reuse file number
  • You must close the file after all data has been
    read or written

58
Sequential File Homework
  • Sequential File Homework
  • Payroll Text Files

59
Renaming Files
  • Rename (oldFileName, newFileName)
  • Must include path if not in Bin\Debug directory

60
Deleting
  • Files
  • Kill (filename)
  • Data
  • __________
  • Open Current File
  • Read data from current file
  • If it is the data to be deleted, do nothing
  • Else, write it to a second, temporary file
  • Loop through steps 2 - 4 until End of current
    file
  • Close both files
  • Delete first file
  • Rename second temporary file with name of first
    file

61
Deleting
  • Files
  • Kill (filename)
  • Data
  • Can this be done???
  • Open Current File
  • Read data from current file
  • If it is the data to be deleted, do nothing
  • Else, write it to a second, temporary file
  • Loop through steps 2 - 4 until End of current
    file
  • Close both files
  • Delete first file
  • Rename second temporary file with name of first
    file

62
Searching
  • Read in Data Element
  • If it is the data you are searching for, you are
    finished
  • Else, read next data element
  • If you reach the end of file
  • Data does not exist

63
Sorting
  • Can this be done???
  • Not Really
  • Read each data item into an array (or parallel
    arrays)
  • Sort the array(s)
  • Write the array out to a second temporary file
  • Close both file
  • Delete first file
  • Rename second file with name of first file

64
Merging
  • Can this be done???
  • Not Really
  • Read both files into an array (or parallel
    arrays)
  • Sort the array(s)
  • Write the array to a new second, temporary file
  • Close both files
  • Delete first file
  • Rename second file with name of first

65
Error Checking
  • Trapping actions that can cause an application to
    fail
  • __________
  • Exceptions that are not handled by code within
    the application
  • Divide by Zero
  • __________
  • Invalid Drive Letter
  • File does not exist
  • Read/Write from a drive that is empty
  • Write to a full drive
  • Etc.

66
Error Checking
  • Trapping actions that can cause an application to
    fail
  • Run-Time errors
  • Exceptions that are not handled by code within
    the application
  • Divide by Zero
  • __________
  • Invalid Drive Letter
  • File does not exist
  • Read/Write from a drive that is empty
  • Write to a full drive
  • Etc.

67
Error Checking
  • Trapping actions that can cause an application to
    fail
  • Run-Time errors
  • Exceptions that are not handled by code within
    the application
  • Divide by Zero
  • Files
  • Invalid Drive Letter
  • File does not exist
  • Read/Write from a drive that is empty
  • Write to a full drive
  • Etc.

68
Structured Exception Handling
  • Allows program to catch and deal with run time
    exceptions
  • Detect and respond to errors that can occur in a
    running program
  • __________
  • Try statement
  • Write all code that __________
  • Code is protected from crashing program
  • Catch statement
  • Write code to deal with problem, display error
    message
  • Will only execute __________ in Try block

69
Structured Exception Handling
  • Allows program to catch and deal with run time
    exceptions
  • Detect and respond to errors that can occur in a
    running program
  • Try Catch statements
  • Try statement
  • Write all code that __________
  • Code is protected from crashing program
  • Catch statement
  • Write code to deal with problem, display error
    message
  • Will only execute __________ in Try block

70
Structured Exception Handling
  • Allows program to catch and deal with run time
    exceptions
  • Detect and respond to errors that can occur in a
    running program
  • Try Catch statements
  • Try statement
  • Write all code that might cause a problem
  • Code is protected from crashing program
  • Catch statement
  • Write code to deal with problem, display error
    message
  • Will only execute __________ in Try block

71
Structured Exception Handling
  • Allows program to catch and deal with run time
    exceptions
  • Detect and respond to errors that can occur in a
    running program
  • Try Catch statements
  • Try statement
  • Write all code that might cause a problem
  • Code is protected from crashing program
  • Catch statement
  • Write code to deal with problem, display error
    message
  • Will only execute if a problem occurs in Try block

72
Catch exceptions
  • Catch exceptionVar as ExceptionType
  • exceptionVar is the variable name for the
    exception
  • ExceptionType is the specific type of exception
  • System.IO.File exception types
  • SecurityException
  • ArgumentException
  • ArguementNullException
  • PathTooLongException
  • DirectoryNotFoundException
  • FileNotFoundException
  • NotSupportedException

73
Finally Clause
  • Code that will execute __________ the Try
    statements (and any necessary catch statements)
  • Code to close file should go here
  • Executes __________ an exception occurs or not

74
Finally Clause
  • Code that will execute after the Try statements
    (and any necessary catch statements)
  • Code to close file should go here
  • Executes __________ an exception occurs or not

75
Finally Clause
  • Code that will execute after the Try statements
    (and any necessary catch statements)
  • Code to close file should go here
  • Executes whether an exception occurs or not

76
Example (generic exception)
  • Private sub btnFileOpen_Click ()
  • Try
  • FileOpen(1, myFile.txt, OpenMode.Input)
  • txtFile.text InputString (1, LOF(1))
  • Catch Err as System.IO.Exception
  • MessageBox.show (The file does not exist on
  • current drive)
  • Finally
  • FileClose(1)
  • End Try
  • End Sub

77
Example (specific exception)
  • Private sub btnFileOpen_Click ()
  • Try
  • FileOpen(1, myFile.txt, OpenMode.Input)
  • txtFile.text InputString (1, LOF(1))
  • Catch Err as FileNotFoundException
  • MessageBox.show (The file does not exist)
  • Catch Err as DirectoryNotFoundException
  • MessageBox.show (This directory cannot be
    found)
  • Catch Err as Exception default, catches all
    other problems
  • MessageBox.show (File Error)
  • Finally
  • FileClose(1)
  • End Try
  • End Sub

78
File Dialog Boxes
  • __________ Windows that appear on screen
  • Obtain information from user
  • __________
  • OpenFileDialog
  • Open files for reading
  • SaveFileDialog
  • Open files for writing

79
File Dialog Boxes
  • Dialog Boxes Windows that appear on screen
  • Obtain information from user
  • __________
  • OpenFileDialog
  • Open files for reading
  • SaveFileDialog
  • Open files for writing

80
File Dialog Boxes
  • Dialog Boxes Windows that appear on screen
  • Obtain information from user
  • File Dialog Boxes -
  • OpenFileDialog
  • Open files for reading
  • SaveFileDialog
  • Open files for writing

81
File Dialog Boxes
  • Add dialog box from the toolbox to the project
  • Will appear in the component tray (not on form)
  • Name the control (ofd or sfd prefix)
  • Display Function ? ControlName.showDialog( )
  • Function with return
  • - DialogResult.OK file name selected
  • - DialogResult.Cancel no file selected

82
Example
  • If ofdOpenFile.ShowDialog( ) DialogResult.OK
    then
  • FileOpen(1, ofdOpenFile.FileName,
    OpenMode.Input)
  • Else
  • MsgBox.show (No File was selected)
  • End If
  • If sfdSaveFile.ShowDialog( ) DialogResult.OK
    then
  • FileOpen(2, sfdSaveFile.FileName,
    OpenMode.Append)
  • Else
  • MsgBox.show (No File was selected)
  • End of

83
Dialog Boxes Properties
  • __________ (Description, Actual Filter Used)
  • Text File (.txt) .txt
  • filter only text files
  • B) Text Files (.txt) .txt All Files
    (.) .
  • Text files and all file extenstion
  • __________ Which directory to look in first
  • __________ Title Bar of dialog Box

84
Dialog Boxes Properties
  • Filter Properties (Description, Actual Filter
    Used)
  • Text File (.txt) .txt
  • filter only text files
  • B) Text Files (.txt) .txt All Files
    (.) .
  • Text files and all file extenstion
  • __________ Which directory to look in first
  • __________ Title Bar of dialog Box

85
Dialog Boxes Properties
  • Filter Properties (Description, Actual Filter
    Used)
  • Text File (.txt) .txt
  • filter only text files
  • B) Text Files (.txt) .txt All Files
    (.) .
  • Text files and all file extenstion
  • Initial Directory Property Which directory to
    look in first
  • __________ Title Bar of dialog Box

86
Dialog Boxes Properties
  • Filter Properties (Description, Actual Filter
    Used)
  • Text File (.txt) .txt
  • filter only text files
  • B) Text Files (.txt) .txt All Files
    (.) .
  • Text files and all file extenstion
  • Initial Directory Property Which directory to
    look in first
  • Title Property Title Bar of dialog Box
Write a Comment
User Comments (0)
About PowerShow.com