VB逐行读取TXT

2024-12-22 19:03:11
推荐回答(4个)
回答1:

Function GetLineStr(FileSpec As String) As String() '获取filespec路径下文件每一行内容,并写入数组函数中
Dim i As Integer
Dim TempStr As String, Temp() As String
Dim FileNum As Integer
FileNum = FreeFile
Open FileSpec For Input As #FileNum
Do While Not EOF(1)
Line Input #FileNum, TempStr
i = i + 1
ReDim Preserve Temp(1 To i) As String
Temp(i) = TempStr
Loop
GetLineStr = Temp
Close #FileNum
End FunctionPrivate Sub Command1_Click() '调用示例
Dim FileSpec As String 'txt路径
Static i As Integer
Dim LineStr() As String
i = 1
If i = UBound(LineStr) Then
MsgBox "文件已结束"
i = 1
End If
FileSpec = "d:\1.txt"
LineStr = GetLineStr(FileSpec)
text1.Text = LineStr(i)
i = i + 1
Next

回答2:

既然同胞都出声了,我还不帮忙吗,兄弟,我刚写了一段代码,你看看能用不

Private Sub Form_Load()
Open "C:\123.txt" For Input As #1
End Sub
Private Sub Command1_Click()
On Error Resume Next
Dim str As String
Line Input #1, str
Text1.Text = str
End Sub

回答3:

Private Sub Command1_Click()
Dim s As String
While Not EOF(1)
Line Input #1, s
Text1.Text = s
Wend
End Sub

Private Sub Form_Load()
Open "d:\new\high.txt" For Input As #1
End Sub

Private Sub Form_Unload(Cancel As Integer)
Close #1
End
End Sub

回答4:

很简单 有很多办法的