0
我正在寫一個程序,讀取包含線的輸入文件:如何在Visual Studios中將數據寫入文件?
Scott Atchison,200,74
該文件包含數據的約30不同的線路。我知道如何閱讀文件。在讀入文件之後,它被拆分,然後需要完成一個計算(我知道該怎麼做)。 但是,我的問題是輸出文件,我只能得到輸出文件的最後一行輸出文件。這就是我現在所擁有的:
Public Class BMI
Dim data As String
Dim strName As String
Dim intWeight As Integer
Dim intHeight As Integer
Dim decBMI As Decimal
Private Sub btnInputFile_Click(sender As System.Object, e As System.EventArgs) Handles btnOpenFile.Click
'User chooses a file
OpenFile.ShowDialog()
'Choose a file name into a label
lblFileInput.Text = OpenFile.FileName
Dim inputFile As New IO.StreamReader(lblFileInput.Text)
Do While (inputFile.Peek() > -1)
data = inputFile.ReadLine
Dim fields() As String = data.Split(",")
strName = fields(0)
intWeight = fields(1)
intHeight = fields(2)
txtData.Text = txtData.Text & data & vbNewLine
Loop
FileClose()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btnSaveFile.Click
SaveFile.ShowDialog()
lblFileOutput.Text = SaveFile.FileName
Dim output As IO.StreamWriter
output = New IO.StreamWriter(lblFileOutput.Text)
output.WriteLine(data)
End Sub
Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
decBMI = (intWeight * 703/(intHeight^2))
data = strName & ", " & decBMI
End Sub
末級
那豈不是寫一行,直到所有的30行讀,還是我失去了一些東西,像一個while循環?任何幫助,將不勝感激。
對不起,我應該更加深入我的代碼。我更新了我的帖子以包含我的整個代碼。 – user2205556 2013-04-26 12:00:33
@ user2205556好吧 - 假設你按btnInputFile,然後按下Button1,我的觀點仍然存在。在btnInputFile_Click()的末尾,「data」包含文件的最後一行。如果接下來執行Button1_Click,則會將該行輸出到該文件。 – 2013-04-26 13:19:58