我正在尝试将我的数组内容写入文件。它是一个数组作为结构。我似乎遇到了问题。写完后我似乎无法读取文件。该应用程序冻结,如果我检查我的txt文件中是否有任何信息,它也会锁定。
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Structure Person
Public strName As String
Public dblHeight As Double
Public dblWeight As Double
End Structure
Private peopleDescription(49) As Person
Dim count As Integer = 0
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
If count <= 49 Then
peopleDescription(count).strName = txtName.Text
Double.TryParse(txtHeight.Text, peopleDescription(count).dblHeight)
Double.TryParse(txtWeight.Text, peopleDescription(count).dblWeight)
count += 1
End If
txtName.Text = String.Empty
txtHeight.Text = String.Empty
txtWeight.Text = String.Empty
txtName.Focus()
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim outFile As IO.StreamWriter
Dim intC As Integer
outFile = IO.File.AppendText("persons.txt")
Do While intC < count
outFile.WriteLine(peopleDescription(intC))
Loop
outFile.Close()
End Sub
Private Sub btnRead_Click(sender As Object, e As EventArgs) Handles btnRead.Click
Dim inFile As IO.StreamReader
Dim strInfo As String
If IO.File.Exists("persons.txt") Then
inFile = IO.File.OpenText("persons.txt")
Do Until inFile.Peek = -1
strInfo = inFile.ReadLine
Loop
inFile.Close()
lblMessage.Text = strInfo
Else
MessageBox.Show("Can't find the persons.txt file", "Person Data", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
End Class
我不知道我错过了什么。如果有人可以提供帮助,我会很感激。
答案 0 :(得分:1)
在启动intC
循环之前,你无法初始化while
,并且你没有在循环中递增它,所以它永远不会改变退出它(假设它在第一个循环中进入这可能会让它“锁定”。
在使用之前先将其设置为起始值。
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim outFile As IO.StreamWriter
Dim intC As Integer = 0
outFile = IO.File.AppendText("persons.txt")
Do While intC < count
outFile.WriteLine(peopleDescription(intC))
intC += 1
Loop
outFile.Close()
End Sub