-
Hi.
I need to swap every two characters in a file.
Eg :
abcdefgh -> badcfehg
The code I'm using at the moment is :
Code:
Option Explicit
Private var_line As String
Private Sub Command1_Click()
If (Text1.Text <> "") And (Text2.Text <> "") Then
Label1.Caption = "Working"
Open Text1.Text For Binary Access Read As #1
Open Text2.Text For Binary Access Write As #2
Do While Loc(1) < LOF(1)
var_line = Input(2, #1)
DoEvents
var_line = Right(var_line, 1) & Left(var_line, 1)
Put #2, , var_line
Loop
Close #2
Close #1
Label1.Caption = "Finished"
End If
End Sub
The code must work in such a way that if it is run again on the file, one gets the original file back.
Eg : f(f(x)) = x
Anyone have a better approach ?
- jamie
-
Read the entire file
Swap everything
Write the entire file
That should be a LOT faster.
Gerco.
-
I was doing that originally, but the files are between 8 and 30 megs. So VB just runs out of memory.
- jamie
-
How about chunks? Read 1000 lines, swap, write in new file, repeat until done, delete old file, rename new file.
Gerco.
-
Well, they are binary files, and each line can contain any number of characters.
So I need to open and write in binary mode to make sure that VB doesnt **** up the characters.
So I cant read in a line.
I can read in a certain number of characters (as below).
- jamie
-
Then read 32k or 1024k of the file and do as above..
Be careful by the way... seek will happily report a current position way beyond the end of file, forcing this:
Code:
Private Sub ReadBytes()
Get #1,buffer
If Seek(1)>=Lof(1) then
bytesread=lof(1)-previousseek
else
bytesread=currentseek-previousseek
endif
End Sub
But hey, who cares about that little inconvenience.
Gerco.