Tutorials to .com

Tutorials to .com » Dotnet » Application » VB Class Module to read and write binary block application, including a paper copy and a file compare binary example.

VB Class Module to read and write binary block application, including a paper copy and a file compare binary example.

Print View , by: iSee ,Total views: 2 ,Word Count: 649 ,Date: Tue, 28 Jul 2009 Time: 4:08 AM

Paper copies of examples: (Text1 source file storage location, Text2 object file storage location )--------------------------------- ---------------------------------------


Private Sub Command3_Click ()
Const BUFFER_SIZE = 40960 * 2 'the provisions of the buffer size
Dim nActual As Long
Dim aBuf (0 To BUFFER_SIZE - 1) As Byte 'the distribution of the buffer zone
Dim tmr As Single 'time variable
Dim lFileLen As Long 'length record files
Dim iFileNum As Integer 'records document
Dim k As Long

tmr = Timer

If Not m_cFileRead.OpenBinary (Text1.Text) Then MsgBox "Open document failed!" & Text1.Text
If Not m_cFileWrite.OpenBinary (Text2.Text) Then MsgBox "Open document failed!" & Text2.Text

'A note of the file and open the file the length of the document used to optimize performance.
lFileLen = m_cFileRead.FileLength
iFileNum = m_cFileRead.FileNumber

k = 0
Do
k = k + 1
If k = 10 Then
k = 0
'pb1 is a progress bar control.
pb1.Value = 100 * (Seek (iFileNum) / lFileLen) 'shows the percentage of
DoEvents
End If
'Read block pointer to the buffer zone and expectations of its number of bytes read, return the actual number of bytes to read,
nActual = m_cFileRead.ReadBlock (VarPtr (aBuf (0)), BUFFER_SIZE)
'Write a block pointer to the buffer zone and expectations of its number of bytes to write.
m_cFileWrite.WriteBlock VarPtr (aBuf (0)), nActual
Loop Until nActual <BUFFER_SIZE 'when the actual number of bytes read is less than the buffer size when you do not need to read, you have read

m_cFileRead.CloseFile 'document
m_cFileWrite.CloseFile

MsgBox "OK! Total time:" & Timer - tmr
End Sub

-------------------------------------------------- -------------------------------------------------- -----------------------------------

Examples of binary file comparison (Text1 kept the first position paper, Text2 storage location of the second file )----------------------------

Private Sub Command2_Click ()

Const BUFFER_SIZE As Long = 4096 * 2 'the provisions of the buffer size
Dim cfRead1 As New CFileRead
Dim cfread2 As New CFileRead
Dim aBuf1 (0 To BUFFER_SIZE - 1) As Byte 'the distribution of the buffer zone
Dim aBuf2 (0 To BUFFER_SIZE - 1) As Byte
Dim nActual As Long
Dim i As Long, lCurPos As Long
Dim tmr As Single
Dim k As Long

tmr = Timer

If Not cfRead1.OpenBinary (Text1.Text) Then MsgBox "open file failed:" & Text1.Text
If Not cfread2.OpenBinary (Text2.Text) Then MsgBox "open file failed:" & Text2.Text

If cfRead1.FileLength <> cfread2.FileLength Then MsgBox "File does not as long as"

lCurPos = 1 'the current byte position to be compared, virtual location, different from the file pointer to read and write.
k = 0
Do
'Read block pointer to the buffer zone and expectations of its number of bytes read, return the actual number of bytes to read,
nActual = cfRead1.ReadBlock (VarPtr (aBuf1 (0)), BUFFER_SIZE)
'Reading the first two blocks of data files
nActual = cfread2.ReadBlock (VarPtr (aBuf2 (0)), BUFFER_SIZE)

For i = 0 To nActual - 1 'comparison buffer
If aBuf1 (i) <> aBuf2 (i) Then MsgBox "of different, location:" & lCurPos: Exit Do
lCurPos = lCurPos + 1 'move to the next byte location to be compared
Next i

k = k + 1
If k = 100 Then
k = 0
Cls
Print Timer - tmr, "", (Seek (cfRead1.FileNumber) / 1024) / (Timer - tmr) & "KB / s"
End If
Loop Until nActual <BUFFER_SIZE

cfRead1.CloseFile 'document
cfread2.CloseFile

MsgBox "OK!" & Timer - tmr

End Sub

-------------------------------------------------- -------------------------------------------------- -----------------------------------



.Net Application Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.