Title: Sequential Access Files
1Sequential Access Files
2Sequential 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
- __________
- __________
3Sequential 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
- __________
- __________
4Sequential 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
- __________
- __________
5Sequential 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
- __________
- __________
6Sequential 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
7Additional 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
8Additional 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
9Additional 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
10Data 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
11Data 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
12Data 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
13Data 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
14Data 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
15Data 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
16Data 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
17Data in File
- Each line can also contain totally different data
- Example
- 3200 Main Street
- Apt. B
- Mobile,Al,36695
-
18Operations with Sequential Files
- __________(Modes Input, Output, Append)
- __________ from the File
- __________ to the File (also Append)
- __________ the File
- __________ Files
- __________ (Data?, Files)
- __________
- __________
- __________
- __________
19Operations with Sequential Files
- Open File (Modes Input, Output, Append)
- __________ from the File
- __________ to the File (also Append)
- __________ the File
- __________ Files
- __________ (Data?, Files)
- __________
- __________
- __________
- __________
20Operations with Sequential Files
- Open File (Modes Input, Output, Append)
- Read Data from the File
- __________ to the File (also Append)
- __________ the File
- __________ Files
- __________ (Data?, Files)
- __________
- __________
- __________
- __________
21Operations 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)
- __________
- __________
- __________
- __________
22Operations 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)
- __________
- __________
- __________
- __________
23Operations 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)
- __________
- __________
- __________
- __________
24Operations 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)
- __________
- __________
- __________
- __________
25Operations 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
- __________
- __________
- __________
26Operations 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
- __________
- __________
27Operations 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???
- __________
28Operations 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???
29File Processing
- 3 Main Steps
- __________
- __________
- __________
- System.IO file libraries
- We must import System.IO to include file
processing - Imports System.IO ? In General Declarations
30File 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
31File 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
32File 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
33Opening 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
34Opening 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
35Opening 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
36Opening 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
37OpenMode.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
38OpenMode.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
39OpenFile.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
40OpenFile.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
41OpenFile.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
42OpenFile.Append
- Write data __________ to the file
- Will write to the __________ of an existing file
- If the file does not exist, it will create it
43OpenFile.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
44OpenFile.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
45Read 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
46Read 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
47Read 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
48Read in Entire File
- InputString (fileNumber, amountToReadIn)
- Will read in a __________
- LOF Length of File
- InputString (fileNumber, LOF(fileNumber))
49Read in Entire File
- InputString (fileNumber, amountToReadIn)
- Will read in a sequence of characters
- LOF Length of File
- InputString (fileNumber, LOF(fileNumber))
50Write 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
51Write 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
52Write 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
53Write 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
54Write 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
55Close 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
56Close 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
57Close 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
58Sequential File Homework
- Sequential File Homework
- Payroll Text Files
59Renaming Files
- Rename (oldFileName, newFileName)
- Must include path if not in Bin\Debug directory
60Deleting
- 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
61Deleting
- 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
62Searching
- 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
63Sorting
- 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
64Merging
- 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
65Error 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.
66Error 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.
67Error 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.
68Structured 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
69Structured 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
70Structured 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
71Structured 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
72Catch 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
73Finally 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
74Finally 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
75Finally 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
76Example (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
77Example (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
78File Dialog Boxes
- __________ Windows that appear on screen
- Obtain information from user
- __________
- OpenFileDialog
- Open files for reading
- SaveFileDialog
- Open files for writing
79File Dialog Boxes
- Dialog Boxes Windows that appear on screen
- Obtain information from user
- __________
- OpenFileDialog
- Open files for reading
- SaveFileDialog
- Open files for writing
80File 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
81File 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
82Example
- 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
83Dialog 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
84Dialog 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
85Dialog 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
86Dialog 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