Search Tools Links Login

Fastest FileLoading Techniquie for simple VB

Posted: 2002-06-01
By: ArchiveBot
Viewed: 69

Filed Under:

VB6 Code Cache

No attachments for this post


In All the other methods I've tried, I run into a barrier(around 170K+) where loading a file suddenly becomes very slow. I've tried many methods found here and in books, and none have proven to work as well as I was looking for.
After a long search I ended up spending some off-time figuring a way out myself. This is what I came up with.(speed increases from 20 seconds@170k to .4 seconds@170k)

Original Author: Dan Violet Sagmiller

Inputs

Parameter 1: Path
Path is the path to the file. If there is no file, the program will return nothing.

Assumptions

Remeber that strings in VB are limited. I believe it is somwhere around 2.1gigs. if you happen to be authoring an application for a machine that can handle that much, you should seriously consider an alternate method.

Returns

It returns a binary string(an ordinary string, just that the bytes contain any character, not just Ansi Text). the file is completely loaded into memory.

Side Effects

Remember that this is probably the fastest way to load a file into memory. if your file is larger than the available memory, then it will become slow, and a major burden on the system.

Code

Public Function LoadBin(Path As String) As String
On Error GoTo hell' isn't that where errors belong?
Dim nfile As String ' This becomes the file memory
Dim i As Long ' temp int
i = FreeFile ' Gets a free file number so that this code doesn't interfere with anything else.
Open Path For Binary As i ' read the file raw
  nfile = String(LOF(i), " ") ' create a string in memory that is the size of the file.
  Get i, , nfile ' in one pass, load the entire file as a single record.
Close i ' clean up the mess
LoadBin = nfile 'set the return value
hell: ' this is where it goes if the code breaks anyway.
End Function


Comments on this post

No comments have been added for this post.

You must be logged in to make a comment.