Results 1 to 5 of 5

Thread: dimming fixed length strings

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2000
    Posts
    2

    Post

    i'm writing an encryption program that separates a file into blocks...in which the number of blocks is in the form n ^ 2, so the number of bytes within each block are the length of the file / n ^ 2. I need to dim a variable for reading and writing bytes of a fixed length, which is the number of bytes within each block. i tried doing dim input1 as string * blocksize but that gave me an error saying that i needed a constant...not a variable. if there's any way to work around this, i'd really like to know. thx.
    -spender

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    Unfortunately, you can't dimention a fixed variable with the uknown size. You have to either use a number or a constant variable (also a number).

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2000
    Posts
    2

    Post

    but the thing is....the value of the blocksize variable does not change during the procedure....so i was wondering if there was any way to circumvent this, so that i could get the result that i wanted. it's not that the variable changes, it's just the result of a computation of 2 other variables which are both determined before the procedure using the blocksize variable is run. any ideas would be appreciated...
    -spender

  4. #4
    Member
    Join Date
    Feb 2000
    Location
    Toronto, Canada
    Posts
    44

    Post

    I found the following code in vbworld articles written by Alex Allen.

    Const ChunkSize = 1024
    'This is the amount (chunk) of data to take each
    'time the file is written to.
    'The larger the quicker but more memory intensive.
    'Here I have set it to 1024 bytes which equals 1KB.

    Dim Data As String

    Public Sub Form_Load

    Open "c:\source" For Binary As #1 'Source
    Open "d:\destination" For Binary As #2 'Destination

    Do Until LOF(1) = Loc(1) Or EOF(1)
    'Will do this loop until the end of the source file.
    Data = ""
    'Resets the size of the Data string.
    MsgBox LOF(1) & vbTab & Loc(1) & _
    vbTab & LOF(1) - Loc(1)
    If LOF(1) - Loc(1) < ChunkSize Then
    'If there is not enough data left for one chunk
    Data = String(LOF(1) - Loc(1), 0)
    'Sets the length of the string; Data.
    Else
    'If there is enough data left for one chunk
    Data = String(ChunkSize, 0)
    End If
    Get #1, , Data
    Put #2, , Data
    Loop

    End Sub



    Click File\directory in the topic area and look for binary files.

  5. #5
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357

    Post Here's an idea

    Maybe you could do something like this. Let's say for the sake of this example, that you have a textbox containing text that you want to put into a file. Let's also say that the block size is unknown at compile time. What if you just take the length of the string in the textbox and divide it by the length of the block using the MOD operator (which gives you the remainder). If the remainder is 0, your string will divide equally into the block length and you are ok. If the remainder is more than 0, just pad the end of it with spaces to make it the correct size.

    In this example, I used a big, multi-line textbox to input text. I used an input box to get the block length from the user (meaning it is unkown at compile time). The one wierd thing I had to do was double the block size when I opened the file, because VB uses 2-byte characters in it's strings (unicode) and the length argument in the Open statement must be in bytes, not VB characters.

    It seems to work just fine. Here's the code:
    Code:
    Option Explicit
    
    Private nBlockLen As Long
    
    Private Sub Form_Load()
        'Get block length from user
        nBlockLen = CLng(InputBox("Enter block length below:"))
    End Sub
    
    Private Sub Command1_Click()
    
        Dim nFileNum As Integer, _
            nNumBlocks As Integer, _
            nCounter As Integer
            
        'If the text is not equally divisible by the block length to start
        'with, then pad the end with spaces so it is evenly divisible
        If Not (Len(Text1) Mod nBlockLen) Then
            Text1 = Text1 & Space(nBlockLen - (Len(Text1) Mod nBlockLen))
        End If
        
        nNumBlocks = Len(Text1) / nBlockLen 'There will be no remainder now
            
        nFileNum = FreeFile
        'Since VB uses 2-byte per character strings, double block len
        'There is probably a better way to do this part.
        Open "c:\my documents\text.txt" For Random As nFileNum Len = nBlockLen * 2
        
        Put #nFileNum, 1, Mid$(Text1, 1, nBlockLen)
    
        For nCounter = 1 To nNumBlocks - 1
            Put #nFileNum, nCounter + 1, Mid$(Text1, nCounter * nBlockLen, nBlockLen)
        Next nCounter
        
        Close #nFileNum
        
    End Sub
    Sorry if this is way off base, it was just an idea I had when I read your post.

    ~seaweed

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