Thursday, April 6, 2017

Random File: Program Code ( visual basic )

Computer Science(9608) notes and Resources










Imports System.IO
Module Module1



    Structure employeerecord

        <VBFixedString(15)> Dim firstname As String
        <VBFixedString(15)> Dim lastname As String
        <VBFixedString(15)> Dim title As String

    End Structure



    Dim employee As employeerecord
    Dim filenumber As Integer
    Dim lastrec As Integer
    Dim position As Integer
    Dim reclength As Long = Len(employee)
    Dim deleted As Boolean = False




    Function randomgetlastrecord() As Integer
        filenumber = FreeFile()
        FileOpen(filenumber, "d:/employee.txt", OpenMode.Random, , , reclength)
        Dim lastrecord As Long
        lastrecord = 1

        Do While Not (EOF(filenumber))

            FileGet(filenumber, employee, lastrecord)
            lastrecord = lastrecord + 1

        Loop

        FileClose(filenumber)
       Return lastrecord

    End Function





    Sub randominputintofile()

        Dim employeefirstname As String
        Dim employeelastname As String
        Dim employeetitle As String

        Dim i As Integer

        For i = 1 To 3

            lastrec = randomgetlastrecord()

            filenumber = FreeFile()
            FileOpen(filenumber, "d:/employee.txt", OpenMode.Random, , , reclength)

            Console.WriteLine("enter firstname")
            employeefirstname = Console.ReadLine()
            Console.WriteLine("enter lastname")
            employeelastname = Console.ReadLine()
            Console.WriteLine("enter title")
            employeetitle = Console.ReadLine()

            employee.firstname = employeefirstname
            employee.lastname = employeelastname
            employee.title = employeetitle

            FilePut(filenumber, employee, lastrec)
            FileClose(filenumber)

        Next

    End Sub





    Sub randomgetfromfile()
        filenumber = FreeFile()
        FileOpen(filenumber, "d:/employee.txt", OpenMode.Random, , , reclength)

        position = 1

        Do While Not EOF(filenumber)

            FileGet(filenumber, employee, position)
            Console.WriteLine(employee.firstname & " " & employee.lastname & " " & employee.title)
            position = position + 1

        Loop

        FileClose(filenumber)

    End Sub

    Sub randomsearchrecord()

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

        filenumber = FreeFile()

        Dim found As Boolean = False
        FileOpen(filenumber, "d:/employee.txt", OpenMode.Random, , , reclength)

        Do While Not EOF(filenumber) And found = False

            FileGet(filenumber, employee, )

            If (Trim(employee.firstname) = sname) Then
                position = Loc(filenumber)
                found = True
            End If

        Loop

        If found = True Then
            Console.WriteLine("record found at position " & position)
            deleted = True

        Else
            Console.WriteLine("no record exits ")
        End If

        FileClose(filenumber)

    End Sub






    Sub randomupdaterecord()

        randomsearchrecord()

        Console.WriteLine("required position is " & position)

        filenumber = FreeFile()
        FileOpen(filenumber, "d:/employee.txt", OpenMode.Random, , , reclength)

        Seek(filenumber, position)

        employee.lastname = "dangol"
        employee.title = "lecturer"

        FilePut(filenumber, employee, )
        FileClose(filenumber)
    End Sub






    Sub randomdeleterecord()

        randomsearchrecord()
        Dim filenumber1, filenumber2 As Integer
        filenumber1 = FreeFile()
        FileOpen(filenumber1, "d:/employee.txt", OpenMode.Random, , , reclength)

        filenumber2 = FreeFile()
        FileOpen(filenumber2, "d:/employeenew.txt", OpenMode.Random, , , reclength)

        Do While Not EOF(filenumber1)

            If (Loc(filenumber1) <> position - 1) Then
                FileGet(filenumber1, employee, )
                FilePut(filenumber2, employee, )
            Else
                FileGet(filenumber1, employee, )
            End If
        Loop

        FileClose(filenumber1)
        FileClose(filenumber2)

        Kill("d:/employee.txt")

        Rename("d:/employeenew.txt", "d:/employee.txt")

        If deleted = True Then
            Console.WriteLine("Record deleted successfully")
        End If


    End Sub





    Sub Main()

        Console.WriteLine("Random file handling")
        Console.WriteLine("")
        Console.WriteLine("enter 1 to add record")
        Console.WriteLine("")
        Console.WriteLine("enter 2 to list record")
        Console.WriteLine("")
        Console.WriteLine("enter 3 to search record")
        Console.WriteLine("")
        Console.WriteLine("enter 4 to edit record")
        Console.WriteLine("")
        Console.WriteLine("enter 5 to delete record")
        Console.WriteLine("")

        Dim choice As Integer
        Console.WriteLine("ENTER YOUR CHOICE")
        choice = Console.ReadLine()
        Select Case choice
            Case 1
                randominputintofile()
            Case 2
                randomgetfromfile()
            Case 3
                randomsearchrecord()
            Case 4
                randomupdaterecord()
            Case 5
                randomdeleterecord()
            Case Else
                Console.WriteLine("invalid entry")
        End Select

        Console.ReadLine()

     End Sub



End Module




                                                           - CIE A-LEVEL COMPUTER SCIENCE (  9608 )

No comments:

Post a Comment

Comments here....