我想用VB读入二进制文件的高程值,但是那个文件有点大(4.7M),用数组来接的话只能保证300*300,那个文件有1200*1200的,不知道用什么办法能够把这么庞大的文件读进数组,文件中因为有一些无效值赋了-9999,以下是我的程序:希望高手指点迷津!
Private Sub Command1_Click()
Dim count1 As Long, count2 As Long, count3 As Long
Dim hijack() As Double '定义存储高程信息的数组
Dim k As Integer
Dim temp(20) As String * 1 '截取长度为1的字符Dim f1 As String * 1
Dim f2 As String * 2
Dim f3 As String * 3
Dim f4 As String * 4
Dim f5 As String * 5CommonDialog1.ShowOpen
Open CommonDialog1.FileName For Binary As #2
count1 = 1
count2 = 1
count3 = 1
ReDim hijack(300, 300)For i = 0 To 300
For j = 0 To 300
For k = 0 To 20
Seek #2, count1
Get #2, , temp(k)
If temp(k) = " " And count2 = 2 Then Seek #2, count3: Get #2, , f1: hijack(i, j) = Val(f1): count3 = count3 + count2 - 1: count2 = 1: count1 = count1 + 1: Exit For
If temp(k) = " " And count2 = 3 Then Seek #2, count3: Get #2, , f2: hijack(i, j) = Val(f2): count3 = count3 + count2 - 1: count2 = 1: count1 = count1 + 1: Exit For
If temp(k) = " " And count2 = 5 Then Seek #2, count3: Get #2, , f4: hijack(i, j) = Val(f4): count3 = count3 + count2 - 1: count2 = 1: count1 = count1 + 1: Exit For
If temp(k) = " " And count2 = 4 Then Seek #2, count3: Get #2, , f3: hijack(i, j) = Val(f3): count3 = count3 + count2 - 1: count2 = 1: count1 = count1 + 1: Exit For
If temp(k) = " " And count2 = 6 Then Seek #2, count3: Get #2, , f5: hijack(i, j) = -1: count3 = count3 + count2 - 1: count2 = 1: count1 = count1 + 1: Exit For ’把-9999转换为-1
count1 = count1 + 1
count2 = count2 + 1
Next k
Next j
Next iFor i = 50 To 100
For j = 50 To 100
Picture1.Print hijack(i, j) & " ";
Next j
Picture1.Print
Next i
Picture1.Print
Close
End Sub
Private Sub Command1_Click()
Dim count1 As Long, count2 As Long, count3 As Long
Dim hijack() As Double '定义存储高程信息的数组
Dim k As Integer
Dim temp(20) As String * 1 '截取长度为1的字符Dim f1 As String * 1
Dim f2 As String * 2
Dim f3 As String * 3
Dim f4 As String * 4
Dim f5 As String * 5CommonDialog1.ShowOpen
Open CommonDialog1.FileName For Binary As #2
count1 = 1
count2 = 1
count3 = 1
ReDim hijack(300, 300)For i = 0 To 300
For j = 0 To 300
For k = 0 To 20
Seek #2, count1
Get #2, , temp(k)
If temp(k) = " " And count2 = 2 Then Seek #2, count3: Get #2, , f1: hijack(i, j) = Val(f1): count3 = count3 + count2 - 1: count2 = 1: count1 = count1 + 1: Exit For
If temp(k) = " " And count2 = 3 Then Seek #2, count3: Get #2, , f2: hijack(i, j) = Val(f2): count3 = count3 + count2 - 1: count2 = 1: count1 = count1 + 1: Exit For
If temp(k) = " " And count2 = 5 Then Seek #2, count3: Get #2, , f4: hijack(i, j) = Val(f4): count3 = count3 + count2 - 1: count2 = 1: count1 = count1 + 1: Exit For
If temp(k) = " " And count2 = 4 Then Seek #2, count3: Get #2, , f3: hijack(i, j) = Val(f3): count3 = count3 + count2 - 1: count2 = 1: count1 = count1 + 1: Exit For
If temp(k) = " " And count2 = 6 Then Seek #2, count3: Get #2, , f5: hijack(i, j) = -1: count3 = count3 + count2 - 1: count2 = 1: count1 = count1 + 1: Exit For ’把-9999转换为-1
count1 = count1 + 1
count2 = count2 + 1
Next k
Next j
Next iFor i = 50 To 100
For j = 50 To 100
Picture1.Print hijack(i, j) & " ";
Next j
Picture1.Print
Next i
Picture1.Print
Close
End Sub
一个疑问,以2进制方式读取文件,那来String数据?
如你那样的文件似乎应使用以随机方式打开文件,文件中含有用户自定义数据类型 Record 的记录。
例:Type Record ' 定义用户自定义数据类型。
ID As Integer
Name As String * 20
End TypeDim MyRecord As Record ' 声明变量。
Open "TESTFILE" For Random As #1 Len = Len(MyRecord)
' 若要以其他方式打开文件,必需先关闭此文件。
Close #1
Dim qg() As String
Dim str As String
Dim i As Long
Dim j As Long
Dim strData As Variant
CommonDialog1.ShowOpen
Open CommonDialog1.FileName For Input As #1
Do While Not EOF(1)
Input #1, strData
'qg(i) = strData
i = i + 1
Loop
j = i
Close #1
ReDim qg(j - 1)
i = 0
Open CommonDialog1.FileName For Input As #1
Do While Not EOF(1)
Input #1, strData
qg(i) = strData
i = i + 1
Loop
j = i
Close #1
For i = 0 To UBound(qg)
Picture1.Print i; qg(i)
Next
End Sub
-9999 -9999 -9999 483 480 474 458 437 417 408 400 388 361 333 319 316 316 312 303 292 289 292 293 291 293 301 303 302 300 295 293 295 298 300 302 300 298 294 290 288 291 300 317 这样的还有结尾是0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
-9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 -9999 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 这样的不知道是否会有影响
Dim str As String
Dim str1 As String
CommonDialog1.ShowOpen
txtEdit = ""
Open CommonDialog1.FileName For Input As #1
Do While Not EOF(1)
Input #1, str1
str = str & str1 & Chr(13) & Chr(10)
Loop
Close #1
Dim qg() As String
Dim i As Integer
Dim j As Integer
qg = Split(str, " ")
i = UBound(qg)
For i = 0 To UBound(qg)
Picture1.Print qg(i);
Next
End Sub
一般来说二进制方式操作文件 把文件一次性或者分批读取到byte数组中 或者自定义类型比较多
文本方式 也可以一次性读取到 string 变量中
比如:
dim str1 as string, strs() as string
str1 = Input(LOF(1), 1)
'然后用split 函数把字符串进行分割存入字符串数组变量中
strs=split(str1," ")
lbound(strs) 数组起始值 一般0
ubound(strs) 数组下标最大值