View
228
Download
0
Category
Preview:
Citation preview
8/8/2019 Manipulating With Data From Files
1/39
1
Manipulating with
Data from Files
Reading and writing text files
8/8/2019 Manipulating With Data From Files
2/39
2
Understanding Computer Files
and How They are Stored Random access memory (RAM): temporary storage
Example: storing a value in a variable Volatile storage
Nonvolatile storage is permanent Example: saving a program to disk
Permanent storage devices: hard disks, floppy disks,magnetic tapes, compact discs
Sequential file: records are stored one after anotherin some order
8/8/2019 Manipulating With Data From Files
3/39
3
Data files contain facts and figures
Program files (application files) storesoftware instructions
Files occupy space on a section of a
storage deviceEach has a name and a creation time
Write to the file: store data in a file on
persistent storage device
8/8/2019 Manipulating With Data From Files
4/39
4
Manipulation with Files
Open the file: locate file on storage device,
associate a variable name with it Read from the file or write to a file: copy data
from the file on storage device into RAM
Close the file: make the file unavailable to theprogram
8/8/2019 Manipulating With Data From Files
5/39
5
8/8/2019 Manipulating With Data From Files
6/39
6
1. Create a text file (using Notepad)
2. Start writing a Program (VB)
3. To read form the file to a program declare and open a
StreamReader.
4. To write to the file from a program declare and open a
StreamWriter.
FILE
PROGRAM
StreamReader
StreamWriter
8/8/2019 Manipulating With Data From Files
7/39
7
Reading from a file (1)
1. Declare a StreamReader
DimsrAs IO.StreamReader
2. Connect the StreamReader with the specified
file (the file you want to read)
sr = IO.File.OpenText(FileName.txt)
* File path. To avoid problems with accessing file
store the text file in the Bin folder of the
project
8/8/2019 Manipulating With Data From Files
8/39
8
Reading from a file (2)
3. Read the file
DimlineAs String
Do While (sr.Peek -1)line = sr.ReadLine()
Loop4. Close the StreamReader
sr.Close()
8/8/2019 Manipulating With Data From Files
9/39
9
DimsrAs IO.StreamReader
DimlineAs String
sr = IO.File.OpenText
(FileName.txt)
Do While (sr.Peek -1)
line = sr.ReadLine()lstBox.Items.Add(line)
Loop
sr.Close()
8/8/2019 Manipulating With Data From Files
10/39
10
Writing into a File
1. Declare a StreamWriterDim sw As IO.StreamReader
2. Connect the StreamWriter with the specified file(the file you want to write into)
sw = IO.File.CreateText(FileName.txt)
or
sw = IO.File.AppendText(FileName.txt)
8/8/2019 Manipulating With Data From Files
11/39
11
Writing into a File
3. Write line into a file:
sw.WriteLine(Hello)
4. Close the StreamWriter
sw.Close()
8/8/2019 Manipulating With Data From Files
12/39
12
Writing into a File
sw = IO.File.CreateText(FileName.txt)
Creates a new file. If file already exists this command will delete old
information, and record the new one.
8/8/2019 Manipulating With Data From Files
13/39
13
Writing into a File
Dim sw As StreamWritersw=IO.File.CreateText(FName.txt)
sw.WriteLine(A)
sw.WriteLine(B)
sw.WriteLine(C)
sw.WriteLine(D)
Sw.Close()
8/8/2019 Manipulating With Data From Files
14/39
14
Writing into a File
sw = IO.File.AppendText(FileName.txt)
This command can work only withalready existing files. It will append
new information to the existing part of
the file.
8/8/2019 Manipulating With Data From Files
15/39
15
File can have only one stream open at a
time. To make sure if a certain stream is open
or not, you can use Flag variable, that willhold the status of the Streams.
Before you open a stream make sure thatthere is no other stream attached to thisfile
And if there is a stream attached close itfirst.
8/8/2019 Manipulating With Data From Files
16/39
16
DimsrAs IO.StreamReader
DimlineAs String
DimsrOpen As Boolean = False
sr = IO.File.OpenText
(FileName.txt)
srOpen = True
Do While (sr.Peek -1)
line = sr.ReadLine()lstBox.Items.Add(line)
Loop
If srOpen = True
sr.Close()EndIf
8/8/2019 Manipulating With Data From Files
17/39
17
Plan for Week 1
1. Create a VB project called Students2. Design and create GUI (name all the controls
properly)
3. Create a text file on Notepad called Students.txt;
add at least 20 entries (records) in the fileMike Connor T127 123-234-2334 mconnor&bell.ca 2.8
Ann Smith T201 416-354-6345 asmith23@rogers.com 2.4
Kate Wheeler T201 416-490-3551 kwheeler@yahoo.com 3.4
Cathy McDonald T127 416-406-3616 cmcdo3@hotmail.com 3.5
Peter Black T127 321-597-2345 pblack34@yahoo.com 3.6
4. Save this file in the project folder under Bin/Debugfolder
8/8/2019 Manipulating With Data From Files
18/39
18
5. In your project provide a functionality (button) to display all records
into a list box.
6. Provide a functionality (button) to display record by record into
separate text boxes.
8/8/2019 Manipulating With Data From Files
19/39
19
Validation Functions
7. Write a separate program (this program is not connected withyour assignment) to validate e-mail, phone number, group, GPA
8/8/2019 Manipulating With Data From Files
20/39
20
Add new record into a file8. In your assignment provide a functionality (button) to add a
student to a file (without validation). You will be able to do itonly on local drives.
9. Make validations into functions and add validations to a project
8/8/2019 Manipulating With Data From Files
21/39
21
Structures
When we need to bind two or more pieces
of related data, we can use STRUCTURE
Structure is a way to package as a singleunit several related variables of different
type.
Structure is a User-Defined Data Type(UDT)
8/8/2019 Manipulating With Data From Files
22/39
22
Structures Declaration
Structure Student
DimnameAs String
DimGPAAs Double
End Structure
The structure must be declaredoutside of all procedures (inside aclass), like a sub or function
name and GPAare members ofStructureStudent
8/8/2019 Manipulating With Data From Files
23/39
23
Structures
1. Declaration of a variable
Dim st1 As Student
2. Manipulating with Structure members:
st1.name = Mary
st1.GPA = 3.9
8/8/2019 Manipulating With Data From Files
24/39
8/8/2019 Manipulating With Data From Files
25/39
25
Student Structure for the Project
Structure Student
Dim first As String
Dim last As String Dim majorAs String
Dim phone As String
Dim email As String
Dim gpa As Double
End Structure
8/8/2019 Manipulating With Data From Files
26/39
26
Dim array() As String
array = line.Split(" ")txtFirst.Text = array(0)
txtLast.Text = array(1)
txtMajor.Text = array(2)txtPhone.Text = array(3)
txtEmail.Text = array(4)
txtGpa.Text = array(5)
8/8/2019 Manipulating With Data From Files
27/39
27
Calculating Number Of Records
Function NumberOfRecords(ByVal fileAs String) As Integer
Dim n As Integer = 0
sr= IO.File.OpenText(filespec)
Do While (sr.Peek -1)
line = sr.ReadLine
n += 1
Loopsr.Close()
Return n
End Function
8/8/2019 Manipulating With Data From Files
28/39
28
Read File Into Array
Private Sub ReadToArray(ByRef s() As Student, ByVal num As Integer)Dim index As Integer
Dim a() As String
Dim record As String
sr = IO.File.OpenText(filename)
For index = 0 To num - 1record = sr.ReadLine
a = record.Split(" ")
s(index).first = a(0)
s(index).last = a(1)
s(index).major = a(2)
s(index).phone = a(3)s(index).gpa = a(4)
Next
sr.Close()
End Sub
8/8/2019 Manipulating With Data From Files
29/39
29
Read File Into Array
Dim n As Integer =
NumberOfRecords(file)
Dim a As Integer
Dim stud(n - 1) As Student
ReadToArray(stud, n)
8/8/2019 Manipulating With Data From Files
30/39
30
Multiple Forms
1. Create both Forms.
2. Give a meaningful name for the second
Form (in the Name property)
frmSearch, frmSort, ect.3. In both form place buttons that allow to
go from one form to the other.
8/8/2019 Manipulating With Data From Files
31/39
31
Form1
frmSource
Name: secondForm
8/8/2019 Manipulating With Data From Files
32/39
32
Public Class Form1
Public n As String
Private Sub
btnCalc_Click(..)n = txtName.Text
Dim secondForm As NewfrmSources()
secondForm.n2 = n
secondForm.ShowDialog()
txtIncome.Text =FormatCurrency
(secondForm.sum)
End Sub
End Class
Public Class frmSources
Public sum As Double
Public n2 As String
Private SubbtnComput_Click(..)
sum = CDbl(txtWages.Text) +CDbl(txtInterest.Text)
+ CDbl(txtDivident.Text)
Me.Close()End Sub
Private SubfrmSources_Load(..)
txtName.Text = n2
End SubEnd Class
8/8/2019 Manipulating With Data From Files
33/39
33
Deleting record from the file. (1)
1. Read the whole file into the array ofstrings or structures
2. Search the array for the particular record
3. Create 2nd array of the same data typeas the 1st, but one size smaller
4. Copy all the elements from the 1st arraybefore the selected element and after theselected element.
5. Write file from the 2nd array.
8/8/2019 Manipulating With Data From Files
34/39
34
Deleting record from the file. (2)
Through Second File1. Open the original file for reading
2. Open new file for writingsw=File.CreateText(Temp.txt)
3. Read from old file, compare line, if the line doesnt matchthe one youve selected to be deleted, then write this lineinto a new file
4. Close both streams
5. Delete old File
File.Delete(Student.txt)
6. Rename new file.
File.Move(Temp.txt,Student.txt)
8/8/2019 Manipulating With Data From Files
35/39
35
Sort file in ascending order:
1. Define number of records in the file (27).2. Create an array of structures (Student).
The size of the array is equals the numberof records in the file.
3. Read (copy) date from the file into thearray (28,29).
4. Sort array in ascending order (36).
5. Write the information from the array intothe file (37).
6. Display the sorted file into the list box
8/8/2019 Manipulating With Data From Files
36/39
36
Sort array in ascending orderDim index As IntegerDim temp As Student
For i = 0 To n - 2
For index = i + 1 To n - 1
If stud(index).last
8/8/2019 Manipulating With Data From Files
37/39
8/8/2019 Manipulating With Data From Files
38/39
38
Search the file
1. Repeat steps 1, 2, and 3 from Sortingprocedure.
2. Search the array (in this example the
search is done by the last name):
8/8/2019 Manipulating With Data From Files
39/39
39
Dim found As Boolean = False
Dim index As Integer
ln = InputBox("Enter Last Name:", "Last Name")
For index = 0 To n 1If stud (index).last = ln Then
found = True
line = stud (index).first & " " &
stud(index).last & " " &
stud(index).major & " " &
stud (index).phone &
" " & stud (index).email & " " &
stud (index).gpa
lstResult.Items.Add(line)
End If
NextIf found = False Then
MsgBox("ITEM WAS NOT FOUND")
End If
Recommended