Thursday, April 6, 2017

Serial File: Program code ( visual basic )

Computer Science(9608) notes and Resources







Writing records to file

Module Module1

    Sub Main()

        Dim sw As IO.StreamWriter
        sw = IO.File.CreateText("d:/staff.txt")

        sw.WriteLine("ram 100")
        sw.WriteLine("shyam 200")
        sw.WriteLine("hari 300")
        sw.WriteLine("sita 400")
        sw.WriteLine("gita 500")

        sw.Close()

        Console.ReadLine()
    End Sub

End Module













Appending records to file



Module Module1

    Sub Main()

        Dim sw As IO.StreamWriter
        sw = IO.File.AppendText("d:/staff.txt")

        sw.WriteLine("ramesh 600")
        sw.WriteLine("suresh 700")
        sw.WriteLine("gopal 800")

        sw.Close()

        Console.ReadLine()
    End Sub

End Module











Reading all records from file

Imports System.IO
Module Module1

    Sub Main()

        Dim staffdtl As String
        Dim sr As StreamReader
        sr = File.OpenText("d:/staff.txt")

        Do Until sr.Peek = -1

            staffdtl = sr.ReadLine()

            Console.WriteLine(staffdtl)

        Loop

        sr.Close()

        Console.ReadLine()
    End Sub

End Module










Searching name in the list

Imports System.IO
Module Module1

    Sub Main()

        Dim staffdtl As String
        Dim found As Boolean = False
        Dim breakvalue() As String

        Dim sr As StreamReader

        sr = File.OpenText("d:/staff.txt")

        Dim sname As String
        Console.WriteLine("enter name to search")
        sname = Console.ReadLine()

        Do Until sr.Peek = -1

            staffdtl = sr.ReadLine()
            breakvalue = staffdtl.Split(" ")
            If breakvalue(0) = sname Then
                found = True
            End If

        Loop

        sr.Close()

        If found = True Then
            Console.WriteLine(sname & " exists in the list")
        Else
            Console.WriteLine("no record exists")
        End If

        Console.ReadLine()
    End Sub

End Module










Deleting record from a file


Imports System.IO
Module Module1

    Sub Main()

        Dim staffdtl As String
        Dim breakvalue() As String

        Dim sr As StreamReader
        sr = File.OpenText("d:/staff.txt")

        Dim sw As StreamWriter
        sw = File.CreateText("d:/staffnew.txt")

        Dim sname As String
        Console.WriteLine("enter name to delete")
        sname = Console.ReadLine()


     

        Do Until sr.Peek = -1

            staffdtl = sr.ReadLine()
            breakvalue = staffdtl.Split(" ")

            If breakvalue(0) <> sname Then
                sw.WriteLine(staffdtl)
            End If

        Loop

        sw.Close()
        sr.Close()

        File.Delete("d:/staff.txt")
        File.Move("d:/staffnew.txt", "d:/staff.txt")

        Console.ReadLine()
    End Sub


End Module




                                                           - CIE A-LEVEL COMPUTER SCIENCE (  9608 )

No comments:

Post a Comment

Comments here....