VB.net 中,我想把TXT的数据文件导入到二维数组中,数据文件的格式是这样的: 1,2,3,4 5,6,7,8

文件格式是1,2,3,45,6,7,8这种形式的
2024-12-18 07:40:21
推荐回答(2个)
回答1:

很简单,代码如下:

Dim f As String = My.Computer.FileSystem.ReadAllText("c:\test.txt") '读取
Strings.Replace(f, vbNewLine, ",") '替换回车符为逗号
Dim a() As String = Split(f, ",") '按逗号分隔字符串,转换为字符串数组
For i = 0 To a.Length - 1 '在立即窗口显示数组内容
Debug.Print(a(i))
Next

回答2:

首先添加一个TextBox。我只对你这一种情况写了代码,具体情况请自行分析!
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal length As Long)
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const EM_GETLINECOUNT = &HBA
Private Const EM_LINEINDEX = &HBB
Private Const EM_LINELENGTH = &HC1
Private Const EM_GETLINE = &HC4
Private Sub Command1_Click()

Dim Arrp()
Open App.Path & "\1.txt" For Binary As #1
Text1.Text = Input(LOF(1), 1)
Close #1

Dim count As Long
count = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0, ByVal 0&)
ReDim Arrp(1 To 4, 1 To count)

Dim pos As Long, str1 As String, length As Integer
Dim i As Long, j As Long
For i = 0 To count
Dim X
pos = SendMessage(Text1.hwnd, EM_LINEINDEX, i, ByVal 0&)

length = SendMessage(Text1.hwnd, EM_LINELENGTH, pos, ByVal 0&)

str1 = String(length, Chr(0))

CopyMemory ByVal str1, length, 2

SendMessage Text1.hwnd, EM_GETLINE, i, ByVal str1

X = Split(str1, ",")
If UBound(X) = 0 Then Exit For
For j = 0 To UBound(X)
Arrp(j + 1, i + 1) = X(j)
Next
Next
End Sub