Results 1 to 7 of 7

Thread: Read from file into buffer (problem)

  1. #1

    Thread Starter
    Registered User Lior's Avatar
    Join Date
    Jan 2000
    Posts
    307

    Unhappy

    Hello all of you out there....

    I have a file, lets say it has a size of 1MB
    I need to encode each byte of the file!
    If I use a loop from 1 to FileSize , It is damn slow!
    I dont want the Processing-Time function to be linear like O(n).

    But when I read everytime 2048 bytes into buffer declared as String * 2048, I need now to make everytime a loop running from 1 to 2048, so I get the same time problem.

    You know what I mean, Lets say the file size is 10K,
    So if I run a loop from 1 to 10240, its just the same to run 5 times a loop from 1 to 2K.

    Any Ideas For Improving The Processing-Time?

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Talking

    LIIIOR, wasn't it the man with the sheshbesh game?

    You can open files in binary and get the whole file into a single variablelength string if you know what I mean
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Boulder, Colorado, USA
    Posts
    325
    use the Input Function and not the statement

    like so:

    Code:
    Dim buffer As String * 2048
    
    '' open the file here in binary mode
    
    '' read in a buffer full
    buffer = Input(2048, #1)
    -Shickadance

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Post

    Try using this one:

    Code:
    Property Get File(filename As String) As String
    Dim fnum As Byte
        fnum = FreeFile
        Open filename For Binary As fnum
            File = Space(LOF(fnum))
            Get #fnum, , File
        Close fnum
    End Property
    Property Let File(filename As String, textstring As String)
    Dim fnum As Byte
        If Dir(filename) <> "" Then Kill filename
        fnum = FreeFile
        Open filename For Binary As fnum
            Put #fnum, , textstring
        Close fnum
    End Property
    And here's an example of how to use it
    Code:
    Msgbox file("C:\autoexec.bat")
    file("C:\autoexec.bat")=file("C:\autoexec.bat") & chr(13) & "echo Cool!!! This one will show up when you restart your computer" & chr(13) & "pause"
    Msgbox file("C:\autoexec.bat")
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5

    Thread Starter
    Registered User Lior's Avatar
    Join Date
    Jan 2000
    Posts
    307

    Thumbs up Thanks....But...

    Hey again...
    Thanks for trying to help me, but guys, thats not what I wanted. I already know how to load the whole file into a buffer with the Input function and also with the Get statement. I think I just didnt explain the problem well enough. So here is the problem well explained:

    It will be easier to explain if I give a practical example.
    So let's say I wanna encrypt some file, with a primitive method, let's say I'll take each byte and rewrite it as the next byte in the ASCII table.

    Let's say I have this simple code:
    Code:
    Dim ByteRead as Byte
    Open "Source" for binary as #1
    Open "Target" for binary as #2
      Do
        Get #1, ,ByteRead
        Put #2, ,Chr$(ByteRead+1)
      Loop Until Loc(1)=lof(1) 'Until End Of File
    Close
    In this example we see that the time that will to the program to be completed is dependent on the source file size.

    If the Source file size is 10MB so the program will do 10*1024*1024 loops ! too much time !

    Now, If I wont read byte after byte as I did, and I will read 2K of data each time using the Get statement, *I still need to access each byte! and make a loop from 1 to 2048 *

    So I will try to ask it in short:
    How can the program processing time not be dependent on the file size?
    In other words,
    How can I access and change each byte in a file individualy,
    without running a loop from 1 To FileSize ?


    Thanks for any help,
    Lior, An Israeli Programmer.

  6. #6
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Boulder, Colorado, USA
    Posts
    325
    Well I think we know what you are trying to do. What we are saying is read a block of the file at a time. Modify that chunk in memory and write it out to a new file. Blocking is much faster than reading a byte at a time and writing back.
    -Shickadance

  7. #7

    Thread Starter
    Registered User Lior's Avatar
    Join Date
    Jan 2000
    Posts
    307

    Wink Thanks

    Thanks man...
    I forgot that reading 10K in these 2 methods is not the same.
    if I read 10K byte after byte so I make 10240 read statements, but if I paste 2K into a buffer, so I do only 5 read statements.

    Thanks for reminding me this.

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