Private Sub Command2_Click()
Dim aryInput() As Byte
Debug.Print Now(), "開始"
ReDim aryInput(FileLen("B:\1.txt") ) '按照檔案長度設定資料讀取記憶體大小
Open "B:\1.txt" For Binary As #1
Get #1, , aryInput
Close #1
Debug.Print Now(), "輸入檔案讀取完畢"
Private Sub Command2_Click()
Dim aryInput() As Byte
Debug.Print Now(), "開始"
ReDim aryInput(FileLen("B:\1.txt") ) '按照檔案長度設定資料讀取記憶體大小
Open "B:\1.txt" For Binary As #1
Get #1, , aryInput
Close #1
Debug.Print Now(), "輸入檔案讀取完畢"
End Sub
檔案很大沒辦法讀取到陣列中
分開讀 Get #1, XXXX , aryInput 指定一下讀寫位置就好了
這樣讀取的都是BYTE吧,要轉TYPE格式的話就比較麻煩了不
uj5u.com熱心網友回復:
一口氣把檔案直接讀入記憶體的例子,然后你再慢慢處理。
Private Sub Command2_Click()
Dim aryInput() As Byte
Debug.Print Now(), "開始"
ReDim aryInput(FileLen("B:\1.txt") ) '按照檔案長度設定資料讀取記憶體大小
Open "B:\1.txt" For Binary As #1
Get #1, , aryInput
Close #1
Debug.Print Now(), "輸入檔案讀取完畢"
End Sub
檔案很大沒辦法讀取到陣列中
分開讀 Get #1, XXXX , aryInput 指定一下讀寫位置就好了
這樣讀取的都是BYTE吧,要轉TYPE格式的話就比較麻煩了不
不麻煩。如果你要轉成字串,用 StrConv 函式就可以了。
uj5u.com熱心網友回復:
一口氣把檔案直接讀入記憶體的例子,然后你再慢慢處理。
Private Sub Command2_Click()
Dim aryInput() As Byte
Debug.Print Now(), "開始"
ReDim aryInput(FileLen("B:\1.txt") ) '按照檔案長度設定資料讀取記憶體大小
Open "B:\1.txt" For Binary As #1
Get #1, , aryInput
Close #1
Debug.Print Now(), "輸入檔案讀取完畢"
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Type GzGdType
GD(3) As Byte
Dx(3) As Byte
End Type
Private Type TerrInfo
GD As Byte
index As Byte
GDtype As GzGdType
G As Byte
G1 As Byte
G2 As Byte
G3 As Byte
G4 As Byte
End Type
Private Sub Command1_Click()
Open "e:\Terr.map" For Random As #15 Len = 15
Open "e:\Terr.map" For Binary As #1 Len = 15
Dim tempdata(300) As TerrInfo
c = GetTickCount()
ReDim TempMapData(300, 300) As TerrInfo
For i = 0 To 300
For j = 0 To 300
Get #15, 2 + (i) + (j) * 5000, TempMapData(i, j)
Next
Next
d = GetTickCount()
c1 = d - c
c = GetTickCount()
For j = 0 To 300
Get #1, 2 + (j) * 5000, tempdata()
Next j
d = GetTickCount()
c2 = d - c
MsgBox ("分個讀取:" & c1 & "區塊讀取:" & c2)
Close #15
Close #1
End Sub
先不說讀取是否正確,反而變慢了呢
uj5u.com熱心網友回復:
你是說后面的300次“區塊讀取”,比前面的90000次“單個讀取”還慢?
感覺不會這樣吧。
uj5u.com熱心網友回復:
這樣讀取的都是BYTE吧,要轉TYPE格式的話就比較麻煩了不
檔案很大沒辦法讀取到陣列中
其實就是懶,我懶得把代碼寫完整,你也懶得改改代碼試一下,好人做到底,按你的type改一下代碼。
Option Explicit
Private Type TyData
a As Long
b As Long
End Type
Private Sub Command1_Click()
Dim aryInput() As TyData
Debug.Print Now(), "開始"
ReDim aryInput(FileLen("B:\1.dat") / 8 - 1) '因為tydata的長度為8個位元組
Open "B:\1.dat" For Binary As #1
Get #1, , aryInput
Close #1
Debug.Print Now(), "輸入檔案讀取完畢"
Private Type DataType
A As Long
B As Long
End Type
Dim FileName$ '檔案名
Dim Istart As Long '起始位元組
Dim Lrec As Long '需讀取的記錄數
Dim DatBuf() As DataType '資料存放陣列
Private Sub Command1_Click()
'讀取資料檔案
Dim L&
ReDim DatBuf(Lrec - 1)
L = Len(DatBuf(0)) * Lrec
If FileLen(FileName) < L + Istart - 1 Then
MsgBox "檔案長度太小!"
Else
Open FileName For Binary As #1
Get #1, Istart, DatBuf
Close #1
'
MsgBox "檔案讀取成功,從" & Istart & "位元組起讀取了" & L & "位元組。"
End If
End Sub
Private Sub Command2_Click()
' 生成DEMO資料
ReDim DatBuf(Lrec - 1)
Dim i&, H$
For i = 1 To Lrec
DatBuf(i - 1).A = Int(Rnd * 10000)
DatBuf(i - 1).B = Int(Rnd * 10000)
Next i
Open FileName For Binary As #1
H = "This is a DEMO !"
Put #1, , H
Put #1, Istart, DatBuf
Close #1
End Sub
Private Sub Form_Load()
FileName = "demo.dat"
Istart = 17 '檔案位元組從1起算
Lrec = 32767
End Sub