Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Text Files

$
0
0
One of the exercises in my AS computing class is to have a user enter student information(name,ID,exam mark) and have it be stored in a .txt file. I have got the add part down but when i want to edit the data i can edit the first one but it overwrites everything in the file. When i try and edit the second record it gives me an error.
Any help?

Imports System.IO
Module Module1

    Dim myfilewriter As StreamWriter
    Dim myFileReader As StreamReader
    Dim myStudentArray() As StudentRec
    Dim studentRecord As StudentRec
    Dim strFileName As String = "student.txt"
    Dim intNumOfRecs As Integer = 0
    Structure StudentRec
        Dim intStudentID As Integer
        Dim strStudentName As String
        Dim intExamMark As Integer
    End Structure

    Sub readRecIntoArray()
        myFileReader = New StreamReader(strFileName)

        intNumOfRecs = myFileReader.ReadLine

        ReDim myStudentArray(intNumOfRecs)

        For intCounter = 1 To intNumOfRecs
            myStudentArray(intNumOfRecs).intStudentID = myFileReader.ReadLine
            myStudentArray(intNumOfRecs).strStudentName = myFileReader.ReadLine
            myStudentArray(intNumOfRecs).intExamMark = myFileReader.ReadLine
        Next

        myFileReader.Close()

    End Sub

    Sub addRecord()

        myfilewriter = New StreamWriter(strFileName, True)

        intNumOfRecs = intNumOfRecs + 1

        myfilewriter.WriteLine(intNumOfRecs)

        ReDim Preserve myStudentArray(intNumOfRecs)

        Console.WriteLine("Enter the students ID number: ")
        myStudentArray(intNumOfRecs).intStudentID = Console.ReadLine()
        Console.WriteLine("Enter the students name: ")
        myStudentArray(intNumOfRecs).strStudentName = Console.ReadLine()
        Console.WriteLine("Enter the students Exam Mark: ")
        myStudentArray(intNumOfRecs).intExamMark = Console.ReadLine()

        myfilewriter.WriteLine(myStudentArray(intNumOfRecs).intStudentID)
        myfilewriter.WriteLine(myStudentArray(intNumOfRecs).strStudentName)
        myfilewriter.WriteLine(myStudentArray(intNumOfRecs).intExamMark)

        myfilewriter.Close()


    End Sub

    Sub editRecord()

        myfilewriter = New StreamWriter(strFileName)

        Dim intEditRec As Integer
        Dim intExam As Integer

        Console.WriteLine("Enter the Record to edit")
        intEditRec = Console.ReadLine()

        ReDim Preserve myStudentArray(intNumOfRecs)

        Console.WriteLine("Current Student ID is: " & myStudentArray(intEditRec).intStudentID)
        Console.WriteLine("Current Student Name is: " & myStudentArray(intEditRec).strStudentName)
        Console.WriteLine("Current Student exam mark is: " & myStudentArray(intEditRec).intExamMark)

        Console.WriteLine("Enter new exam mark: ")
        intExam = Console.ReadLine()

        myStudentArray(intEditRec).intExamMark = intExam

        myfilewriter.WriteLine(myStudentArray(intEditRec).intExamMark)


        Console.ReadLine()

        myfilewriter.Close()


    End Sub
    Sub Main()

        Dim strMenu As String

        Do
            If My.Computer.FileSystem.FileExists("student.txt") = True Then
                Call readRecIntoArray()
            End If




            Console.WriteLine("Press 'A' to add a record")
            Console.WriteLine("Press 'E' to edit a record")
            Console.WriteLine("Press 'D' to display all record")
            Console.WriteLine("Press 'C' to calculate the average class mark")
            Console.WriteLine("Press 'G' to calculate exam grade")
            Console.WriteLine("Press 'R' to delete a record")
            Console.WriteLine("Press 'S' to sort the records into exam order")
            Console.WriteLine("Press 'Q' to quit")
            strMenu = Console.ReadLine

            Select Case strMenu

                Case "A"
                    Call addRecord()
                Case "E"
                    Call editRecord()
                Case "D"

                Case "C"

                Case "G"

                Case "R"

                Case "S"

            End Select

        Loop Until strMenu = "Q"

    End Sub

End Module


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>