user10663279
user10663279

Reputation:

Saving a Structure to a Binary File

I am creating an application using a windows forms application in visual studio in the vb.net language. I need help converting a structure that I coded into a binary file that is essentially a save in user results. I'm not a very good coder so excuse the poor code.

The code below shows that I have created a structure called saveresults and by clicking button1, it should get the contents of the binary file and edit them to be the new result. When I run the code the problem seems to be in the line FileOpen(1, "/bin/debug/1.txt", OpenMode.Binary) in the saveres subroutine.

Structure saveresults 'Structure for saving results
        Dim numright As Integer
        Dim numwrong As Integer
        Dim totalnum As Integer
End Structure
'Subroutine aimed at getting stats saved to a text file to eventually be displayed to the user

Sub saveres(saveresults As saveresults, correct As Boolean)
    saveresults.totalnum = saveresults.totalnum + 1
    'Determining the contents to be saved to the binary file
    If correct = True Then
        saveresults.numright = saveresults.numright + 1
    ElseIf correct = False Then
        saveresults.numwrong = saveresults.numwrong + 1
    End If
    FileOpen(1, "/bin/debug/1.txt", OpenMode.Binary)
    FilePut(1, saveresults)
    FileClose(1)

End Sub

'attempt at saving results to the binary file

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim correct = True
    Dim results As saveresults
    FileOpen(1, "/bin/debug/1.txt", OpenMode.Binary)
    FileGet(1, results)
    saveres(results, correct)
    FileClose(1)
End Sub

Any help would be appreciated. Thank you.

Upvotes: 0

Views: 1278

Answers (2)

Mary
Mary

Reputation: 15091

You are referring to text files and binary files as if they are the same thing. They are not. Text files are human readable in Notepad; binary files are not.

I have not used the methods you are attempting since VB 6. Use the .Net System.IO methods. To use these you need to add Imports System.IO at the very top of your code file.

I have broken your code into Subs and Functions that have a single purpose. Reading the file, writing the file, updating the data, and displaying the data. This makes code more maintainable. If your code misbehaves it is easier to find the error and easier to fix if a method has only one thing to do.

The file location in the example is in the same directory as your .exe file. Probably /bin/Degug.

'A Form level variable to hold the data
Private results As New saveresults

Structure saveresults 'Structure for saving results
    Dim numright As Integer
    Dim numwrong As Integer
    Dim totalnum As Integer
End Structure
'Update the data
Private Sub UpdateResults(Correct As Boolean)
    'It is not necessary to include = True when testing a Boolean
    If Correct Then
        'this is a shortcut method of writin results.numright = results.numright + 1
        results.numright += 1
        'A Boolean can only be True or False so if it is not True
        'it must be False so, we can just use an Else
        'No need to check the condition again
    Else
        results.numwrong += 1
    End If
    results.totalnum += 1
End Sub
'Write text file
Private Sub SaveResultsFile(results As saveresults, correct As Boolean)
    Dim sb As New StringBuilder
    sb.AppendLine(results.numright.ToString)
    sb.AppendLine(results.numwrong.ToString)
    sb.AppendLine(results.totalnum.ToString)
    File.WriteAllText("1.txt", sb.ToString)
End Sub
'Read the text file
Private Function ReadResultsFile() As saveresults
    Dim ResultsFiLe() = File.ReadAllLines("1.txt")
    Dim results As New saveresults With
    {
       .numright = CInt(ResultsFiLe(0)),
       .numwrong = CInt(ResultsFiLe(1)),
       .totalnum = CInt(ResultsFiLe(2))
    }
    Return results
End Function
'Display
Private Sub DisplayResults()
    Dim ResultsToDisplay As saveresults = ReadResultsFile()
    'The $ indicates an interpolated string, the same thing can be accomplished with String.Format
    Label1.Text = $"The correct number is {ResultsToDisplay.numright}. The wrong number is {ResultsToDisplay.numwrong}. The total is {ResultsToDisplay.totalnum}."
End Sub

Upvotes: 0

Precious Uwhubetine
Precious Uwhubetine

Reputation: 3007

Use this instead

 FileOpen(1, "1.txt", OpenMode.Binary)

Using the above opens the file in your project's debug folder.

Upvotes: 1

Related Questions