Results 1 to 6 of 6

Thread: Wroking with files ... fast

  1. #1

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    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]

  2. #2
    Guest
    Read the entire file
    Swap everything
    Write the entire file

    That should be a LOT faster.

    Gerco.

  3. #3

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    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]

  4. #4
    Guest
    How about chunks? Read 1000 lines, swap, write in new file, repeat until done, delete old file, rename new file.

    Gerco.

  5. #5

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    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]

  6. #6
    Guest
    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
  •  



Click Here to Expand Forum to Full Width