Earlier this week I provided a tip that would reverse a
string. This tip demonstrates how to reverse a whole file. What this
code will do is to reveres the content of the file
fromfile.dat and place reversed content it in to tofile.dat.
Create two files fromfile.dat and
tofile.dat. Write the following in the fromfile.dat "The information here will be reversed in the
tofile.". Now place both files in the c:\ directory.
Add two text boxes and one command button to the form. Then cute and paste the code below and run the program.
===============================================================
Private Sub Command1_Click()
Call reversefile("c:\fromfile.dat", "c:\tofile.dat")
ReadToFile
End Sub
Public Sub reversefile(FromFile As String, tofile As String)
Dim mybyte() As Byte
Dim reversedbyte() As Byte
Dim reversebyte As Long
Open FromFile For Binary As #1
Open tofile For Binary As #2
ReDim mybyte(1 To LOF(1)) As Byte
ReDim reversedbyte(1 To LOF(1)) As Byte
Get #1, , mybyte
For reversebyte = UBound(mybyte) To 1 Step -1
reversedbyte(reversebyte) = mybyte(UBound(mybyte) - _
reversebyte + 1)
Next
Put #2, , reversedbyte
Close #2
Close #1
End Sub
Public Sub ReadToFile()
Dim numFile As Integer
Dim lenFile As Integer
numFile = FreeFile
Open "c:\tofile.dat" For Input As numFile
lenFile = LOF(numFile)
Text1 = Input(lenFile, #numFile)
Close #numFile
End Sub
Public Sub FromFile()
Dim FnumFile As Integer
Dim FlenFile As Integer
FnumFile = FreeFile
Open "c:\fromfile.dat" For Input As FnumFile
FlenFile = LOF(FnumFile)
Text2 = Input(FlenFile, #FnumFile)
Close #FnumFile
End Sub
Private Sub Form_Load()
FromFile
End Sub