Group 5 Presentation[1][1]
Group 5 Presentation[1][1]
The ReadLine method reads text from a file line by line, counting
from the current position until the end of the line, depending on the
file's data storage.
A while Loop is typically used to determine the number of items in a
file if it is not specified in the question
Opening the file for input
STEP 1
The InputOutput(IO) class's streamreader data type is utilized to read
a stream of characters from a memory location from a file.
Dim variable name as IO. StreamReader
The StreamReader File specification establishes a communication
link between the computer and the disk drive, identifying the file to
be read
STEP 2
Reading a data item from a file is done using the readline builtin
function.
StrVar = readerVar.Readline.
The data can be assigned to a numeric variable, in that case we use
Val function.
numVar = Val (readerVar.ReadLine)
StreamReader initializes an input marker at file start, moves to next
line at file end, and ends the file after reading the last line.
After reading from the file, the last step is to terminate the
communication link that was set up in Step 1.
readVar.Close()
DAILY USE
Reading from a file line by line, this is commonly used in schools when
they want to create a class list or to create a list for admitted students.
It is also used to take out numbers from a set of numbers e.g taking
prime numbers or integers.
Do’s Don’t
Dim variable name as IO. Do not make decisions about the
StreamReader contents of the file based on the
name of the file
End Sub
End Sub
End Class
Reading a Delimited File
What is a DELIMITED FILE ?
A delimited file is a text file used to store data and data in a delimited
text file is arranged in rows and columns, with one entry per row. Each
column is separated from the next by a field separator character/
a delimiter.
End Using
End Sub
Private Sub LitterData_Load(sender As System.Object, e As System.EventArgs)
Handles MyBase.Load
ListBox1.Text = "Litter Collection Data From Different Schools"
End Sub
End Class
Reading Delimited Files Algorithms:
Check Your DP:
Create Boolean Function stuAvail() that:
Uses a Text field parser to read the marks.txt file.
Declares variables for the student number and the Boolean
to check if the student number appears in the file.
Sets the type of file being read.
Sets delimiter.
Uses while loop to read all fields and lines of the file and
uses and If statement to check if the student number is in the
file.
Create Integer Function calcDP() that takes your student number as
an argument:
Reads the file marks.txt like the previous function.
Declares variables for the student number and different
assessment marks recorded in the txt file.
Declares a variable that will store the DP.
Uses a while loop to read through the text file.
Sets the different variables to the different data values from the text
file.
Uses an If statement to search for the student number and when it
finds it, it uses the other values to calculate the DP.
Returns DP
Uses the two functions to calculate your DP from inputting your
student number.
EXAMPLE CODE 3
Public Class CheckYourDP
Function stuAvail(ByVal stuNum As Integer) As Boolean
Using marksDB As New
Microsoft.VisualBasic.FileIO.TextFieldParser("C:\
Users\HP\Desktop\Group 5 Presentation\Reading
Delimited Files\marks.txt")
Dim currentStudent As String()
Dim studentNumber As Integer
Dim studentAvailability As Boolean
marksDB.TextFieldType =FileIO.FieldType.Delimited
marksDB.SetDelimiters(",")
Cont…
While Not marksDB.EndOfData
currentStudent = marksDB.ReadFields()
studentNumber =Val(currentStudent(0))
If studentNumber = stuNum Then
studentAvailability = True
Else
studentAvailability = False
End If
End While
Return studentAvailability
End Using
End Function
EXAMPLE CODES
Function calcDP(ByVal student_no As Integer) As Single
Using marksDB As New
Microsoft.VisualBasic.FileIO.TextFieldParser("C:\Users\HP\Desktop\Group
5 Presentation\Reading Delimited Files\marks.txt")
Dim currentStudent As String()
Dim studentNumber, assMark, pracMark, test1, test2 As
Integer
Dim DP As Single
marksDB.TextFieldType = FileIO.FieldType.Delimited
marksDB.SetDelimiters(",")
Cont…
While Not marksDB.EndOfData
currentStudent = marksDB.ReadFields()
studentNumber = Val(currentStudent(0))
assMark = Val(currentStudent(1))
pracMark = Val(currentStudent(2))
test1 = Val(currentStudent(3))
test2 = Val(currentStudent(4))
If studentNumber = student_no Then
DP = ((Val(assMark) / 100 * 100) * 0.1)
((Val(pracMark) / 20 * 100) * 0.2) + ((Val(test1) / 50 * 100)
* 0.35) + ((Val(test2) / 50 * 100) * 0.35)
End If
End While
Return DP
End Using
End Function
EXAMPLE CODE 4
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles
Button1.Click
Dim your_stuNum As Integer = TextBox1.Text
checkAvailability = True
Else
TextBox1.Clear()
checkAvailability = False
End If
End Sub
Output to a File
What is an output ?
Output means to produce results, results that can be displayed on
screen, written to a printer or to a file, in this topic we will be focusing
on outputting to a file.
FileWriter As IO.StreamWriter
Where : FileWriter(Variable)
IO(Input Output Class)
Streamwriter(Datatype)
Cont…
To initialise the Streamwriter we need to give it the file name and
location in following manner
This can be initialised in one line like other Variables in the following
way
When we’re inserting Data to the file we use the Write Function or the
Write.Line Function.
The write Function will write all the data to a sequential file without
separating the data since the write function does not separate the
data you use the write.Line Function which writes data in the textfile
line by line.
The Write Function has the following format:
The Write.Line function has the following format different from the
write function
you can either use the str or num, where str is string and num stands
for numerical value.
Step3: Close the file
After you have written all the data you want to the file, you close the
file using the following format :
fw.close()
Do’s Don’t
It advisable to use Write.Line Don't forget to close the file
function when you having a lot of
information to insert into your file
that uses different lines or rather
have separators
For example when you want to
write the list of students taking
the CSC module, you will use the
Write. Line function.
Do’s Don’t
it is imperative to close the file as If you don’t close the file, all the
it makes sure that all your data is information you have written will
saved. erased.
{}
Writing Delimited Files Algorithms:
Sign Up details storer:
Declare a variable for the stream writer.
Declare variables for name, surname, email, phone number, password and re-
entering the password.
Use the variables to store values from different text boxes you use to collect
information.
Use an If statement to check if the two passwords match.
If the do write the values of the variables in the generated file.
If they do not match, clear the text boxes used to collect the two passwords
so you can re enter them.
EXAMPLE CODE : 5
Public Class signUp
name = TextBox1.Text
surname = TextBox2.Text
email = TextBox3.Text
phone = TextBox4.Text
pass = TextBox5.Text
rePass = TextBox6.Text
Cont…
If pass = rePass Then
accountWriter.WriteLine(name & "," & surname & "," & email & "," & phone &
"," & pass)
Me.Close()
Else
Label7.Text = "Passwords do not match"
TextBox5.Clear()
TextBox6.Clear()
End If
accountWriter.Close()
End Sub
Writing Mark down Files Algorithms:
Bank Statement Generator:
Declare variable ID to collect the identity number from a text box.
Use A Stream reader to read bankDetails.txt.
Declare a variable for the StreamWriter, that sets the second argument to True to create
a new file.
Declare variables for all the information stored in the bankDetails.txt file.
Set the text field type and the delimiter.
Use a while loop to read through the file and set each variable name to a different data
value from the text file.
Us If statement to search for the ID number in the text file.
If the ID number is found use the stream writer variable to write the details that are on the
same field as the ID number in different lines in the generated text file.
Close the stream writer.
EXAMPLE CODE 6
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles
Button1.Click
Dim ID As Integer = TextBox1.Text
bsWriter.Flush()