以下是我在项目中使用的一段代码:
Using Reader As New Microsoft.VisualBasic.FileIO.TextFieldParser(OpenFileDialog1.FileName.ToString())
Reader.TextFieldType = FileIO.FieldType.Delimited
Reader.SetDelimiters("\t")
Dim currentRow As String()
Dim valueArray() As Double = {0, 0, 0, 0}
Dim power3, power2, power1, constVar As Double
power3 = 0.0
power2 = 0.0
power1 = 0.0
constVar = 0.0
While Not Reader.EndOfData
Try
currentRow = Reader.ReadFields()
Dim currentString As String
Dim i As Integer = 0
Dim j As Integer = 0
For Each currentField As String In currentRow
currentString = currentField(0)
MsgBox(currentField)
MsgBox(currentString)
Next
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
End Try
End While
End Using
我正在读取的文本文件包含用制表符分隔的浮点值,如下所示:
0.5 0.6 0.7 0.8
但是,现在,当我运行代码时,我将完整的行作为字符串"0.5 0.6 0.7 0.8"
我无法提取每个浮点值。请提出一些提取每个值的方法,以便我可以单独存储它们。