将结构保存到二进制文件

时间:2019-02-05 08:21:33

标签: vb.net visual-studio winforms

我正在使用Visual Studio中以vb.net语言编写的Windows窗体应用程序创建应用程序。我需要帮助将我编码的结构转换为二进制文件,这实际上可以节省用户结果。我不是一个很好的编码员,请原谅可怜的代码。

下面的代码显示我已经创建了一个名为saveresults的结构,并且通过单击button1,它应该获取二进制文件的内容并将其编辑为新结果。当我运行代码时,问题似乎出在FileOpen(1, "/bin/debug/1.txt", OpenMode.Binary)子例程的saveres行中。

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

任何帮助将不胜感激。谢谢。

2 个答案:

答案 0 :(得分:1)

改为使用此

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

使用上面的命令在项目的调试文件夹中打开文件。

答案 1 :(得分:0)

您指的是文本文件和二进制文件,好像它们是同一回事。他们不是。文本文件在记事本中易于阅读;二进制文件不是。

自VB 6起,我没有使用过您尝试的方法。请使用.Net System.IO方法。要使用这些文件,您需要在代码文件的最顶部添加Imports System.IO。

我已将您的代码分为具有单一目的的Subs和Function。读取文件,写入文件,更新数据并显示数据。这使代码更易于维护。如果您的代码行为不当,则仅一种方法要做的事情就更容易发现错误并且更容易解决。

示例中的文件位置与.exe文件位于同一目录中。大概 / 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