|
-
May 31st, 2006, 09:42 PM
#1
Thread Starter
PowerPoster
[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? 
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.
-
Jun 1st, 2006, 07:31 AM
#2
Thread Starter
PowerPoster
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? :-)
-
Jun 1st, 2006, 10:36 AM
#3
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
-
Jun 1st, 2006, 10:50 AM
#4
Thread Starter
PowerPoster
Re: Problems loading specific block of data from a file
 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 :-)
-
Jun 1st, 2006, 03:56 PM
#5
Thread Starter
PowerPoster
Re: Problems loading specific block of data from a file
No-one got any idea or assistance?
-
Jun 1st, 2006, 04:43 PM
#6
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
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
-
Jun 1st, 2006, 05:00 PM
#7
Thread Starter
PowerPoster
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.
-
Jun 2nd, 2006, 01:29 AM
#8
Thread Starter
PowerPoster
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?
-
Jun 2nd, 2006, 03:51 AM
#9
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.
Pete
No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.
-
Jun 2nd, 2006, 08:06 AM
#10
Thread Starter
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|