|
-
Feb 23rd, 2001, 04:46 AM
#1
Thread Starter
Retired VBF Adm1nistrator
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
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Feb 23rd, 2001, 04:48 AM
#2
Read the entire file
Swap everything
Write the entire file
That should be a LOT faster.
Gerco.
-
Feb 23rd, 2001, 07:24 AM
#3
Thread Starter
Retired VBF Adm1nistrator
I was doing that originally, but the files are between 8 and 30 megs. So VB just runs out of memory.
- jamie
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Feb 23rd, 2001, 07:30 AM
#4
How about chunks? Read 1000 lines, swap, write in new file, repeat until done, delete old file, rename new file.
Gerco.
-
Feb 23rd, 2001, 07:31 AM
#5
Thread Starter
Retired VBF Adm1nistrator
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
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Feb 23rd, 2001, 07:36 AM
#6
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|