[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:
Function blockload(filename As String, Optional blocksize As Double, Optional block As Single)
Dim fre As Integer
Dim start As Double
Dim data As String
If blocksize = 0 Then blocksize = 50000
If block = 0 Then block = 1
'calculate start of block to get
start = (blocksize * (block - 1)) + 1
fre = FreeFile
Open filename For Random As #fre Len = blocksize
Get #fre, start, data
blockload = data
Close #fre
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? :bigyello:
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 :-)
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? :-)
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?
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 :-)
Re: Problems loading specific block of data from a file
No-one got any idea or assistance?
Re: Problems loading specific block of data from a file
VB Code:
Function blockload(filename As String, Optional blocksize As Double, Optional block As Single)
Dim fre As Integer
Dim start As Double
Dim data As [color=red]Byte(100)[/color]
If blocksize = 0 Then blocksize = 50000
If block = 0 Then block = 1
'calculate start of block to get
start = (blocksize * (block - 1)) + 1
fre = FreeFile
Open filename For [color=red]Binary[/color] As #fre Len = blocksize
Get #fre, start, data
blockload = data
Close #fre
End Function
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 :-)
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?
Re: Problems loading specific block of data from a file
You need to pre-allocate the data buffer. Try this:
VB Code:
Function blockload(filename As String, Optional blocksize As Double, Optional block As Single) As String
Dim fre As Integer
Dim start As Double
Dim data As String
If blocksize = 0 Then blocksize = 50000
[color=red]data = [color=red]String[/color]$(blocksize, " ")[/color]
If block = 0 Then block = 1
'calculate start of block to get
start = (blocksize * (block - 1)) + 1
fre = FreeFile
Open filename For Binary As #fre
Get #fre, start, data
blockload = data
Close #fre
End Function
When you open files in Binary mode, Len has no effect.
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!