Results 1 to 10 of 10

Thread: [RESOLVED] Problems loading specific block of data from a file

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Resolved [RESOLVED] Problems loading specific block of data from a file

    I am trying to write a function where i give it a file (full path), block size and block number and it gets the given block from the file and copies it to a string...however, i am having problem after problem. For the purpose I am writing this, it *needs* to be able to load a random block and not keep loading blocks again and again until it gets to the required block...time is critical in this case...

    Anyway, a friend gave me http://www.other-space.com/vb/part2/files.html which (under random access) gives a program that uses len= when opening the file, which seemed perfect to me :-) ...I modified it a little to fit my needs, thus:

    VB Code:
    1. Function blockload(filename As String, Optional blocksize As Double, Optional block As Single)
    2. Dim fre As Integer
    3. Dim start As Double
    4. Dim data As String
    5.  
    6. If blocksize = 0 Then blocksize = 50000
    7. If block = 0 Then block = 1
    8.  
    9. 'calculate start of block to get
    10. start = (blocksize * (block - 1)) + 1
    11.  
    12. fre = FreeFile
    13. Open filename For Random As #fre Len = blocksize
    14. Get #fre, start, data
    15. blockload = data
    16. Close #fre
    17. End Function

    When I run this, it stops at the GET line and returns the error 59, "bad record length". I am using test data and the file opened is over 39k and I am using blocksize 100 and block 1.

    Has anyone got suggestions for alternative ways to achieve what I want to do (I understand that for someone who knows about file access it is a simple matter, but one of my main "failures" of VB is using GET correctly :-)) or perhaps got a fix for this code?

    If i gave this file a filename and said block size 100 and block 1, I want it to return the first 100 bytes of the file...if I changed block to 2 I would want the *NEXT* 100 blocks after that...and so on :-)
    Last edited by smUX; May 31st, 2006 at 10:16 PM.

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Problems loading specific block of data from a file

    So no-one has any idea? Could you post feedback please, like what you need from me to be able to actually help me? :-)

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Problems loading specific block of data from a file

    Could be because your blocksize is larger than your file.

    Is the file a fixed-record-length data file or a text file?
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Problems loading specific block of data from a file

    Quote Originally Posted by Al42
    Could be because your blocksize is larger than your file.

    Is the file a fixed-record-length data file or a text file?
    As quoted above, the blocksize is 100 bytes and the filesize is 39k (39,000+ bytes) so it isn't that :-)

    The file itself is actually a text file which i would guess would be the reason why I can't get it to work...I actually need to load in specific length blocks from a binary file and can't seem to get it to work whatever way I do it...the plan is for me to load a file into memory in blocks, but I *don't* want the whole file in memory because it is possible I will be loading 1GB+ files.

    I assume that loading all the blocks up to the one I want would work, but it'd slightly slow down the process as I am sure you can understand loading blocks 100 bytes (or in my case, it'll probably be 50k) at a time would be time consuming :-)

    I guess I could split the file into files within a folder, but it'd be less efficient than the code I am asking for help with :-)

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Problems loading specific block of data from a file

    No-one got any idea or assistance?

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Problems loading specific block of data from a file

    VB Code:
    1. Function blockload(filename As String, Optional blocksize As Double, Optional block As Single)
    2. Dim fre As Integer
    3. Dim start As Double
    4. Dim data As [color=red]Byte(100)[/color]
    5.  
    6. If blocksize = 0 Then blocksize = 50000
    7. If block = 0 Then block = 1
    8.  
    9. 'calculate start of block to get
    10. start = (blocksize * (block - 1)) + 1
    11.  
    12. fre = FreeFile
    13. Open filename For [color=red]Binary[/color] As #fre Len = blocksize
    14. Get #fre, start, data
    15. blockload = data
    16. Close #fre
    17. End Function
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Problems loading specific block of data from a file

    Only change I see is "Dim data As Byte(100)"...and adding the (100) causes the program to refuse to run...removing the (100) but keeping byte (I had string, I know) doesn't seem to work either, it returns a single number (I assume the number is the ascii value of the first or last digit, dunno which)

    So still no luck :-)

    Edit: Oh, and changing random to binary in the open statement :-)
    Last edited by smUX; Jun 2nd, 2006 at 01:28 AM.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Problems loading specific block of data from a file

    I would have thought this was a simple thing to do...does no-one know how to do it?

  9. #9
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Problems loading specific block of data from a file

    You need to pre-allocate the data buffer. Try this:
    VB Code:
    1. Function blockload(filename As String, Optional blocksize As Double, Optional block As Single) As String
    2.  
    3.     Dim fre As Integer
    4.     Dim start As Double
    5.     Dim data As String
    6.    
    7.     If blocksize = 0 Then blocksize = 50000
    8.     [color=red]data = [color=red]String[/color]$(blocksize, " ")[/color]
    9.     If block = 0 Then block = 1
    10.    
    11.     'calculate start of block to get
    12.     start = (blocksize * (block - 1)) + 1
    13.    
    14.     fre = FreeFile
    15.     Open filename For Binary As #fre
    16.     Get #fre, start, data
    17.     blockload = data
    18.     Close #fre
    19.    
    20. End Function
    When you open files in Binary mode, Len has no effect.
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Problems loading specific block of data from a file

    This next line may sound a bit noobish (and it is) but it's how I feel :-)

    YAAAAAAAY, IT WORKED! Thanks!

    Probably my fault...the original site was using the random method but that was refusing to work for me. At one point I did try pre-allocating the string...or at least I thought so...I checked my IM history (I asked a friend if he saw any problems) and it looks like I *actually* used "data = Space$(blocksize)" :-)

    Thanks again...this is an important part of a project I am working on and makes the whole thing a lot easier!

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