VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Fastest FileLoading Techniquie for simple VB

by Dan Violet Sagmiller (2 Submissions)
Category: Files/File Controls/Input/Output
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (4 Votes)

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)

Inputs
Parameter 1: Path Path is the path to the file. If there is no file, the program will return nothing.
Assumes
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.
Code 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.

Rate Fastest FileLoading Techniquie for simple VB

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

Download this snippet    Add to My Saved Code

Fastest FileLoading Techniquie for simple VB Comments

No comments have been posted about Fastest FileLoading Techniquie for simple VB. Why not be the first to post a comment about Fastest FileLoading Techniquie for simple VB.

Post your comment

Subject:
Message:
0/1000 characters