Solved

VB5: Fast Text File Loading

Posted on 2000-04-14
19
247 Views
Last Modified: 2010-05-02
Is there a faster way, be it vb or windows api to load a file into a textbox?
Right now I'm using this code which is not even line by line and is way too slow.  I don't even need to scan the text for anything in this routine...just load the entire thing as quickly as possible into a textbox.


Open tmpfile For Binary As #1
For AC = 1 To LOF(1)
     Get #1, AC, Dummy
     Text2.Text = Text2.Text & Asc(Dummy) & ","
Next AC
Close #1
0
Comment
Question by:ChrisK
  • 6
  • 4
  • 4
  • +3
19 Comments
 
LVL 1

Accepted Solution

by:
Ask_ED earned 50 total points
ID: 2718056
Hi Chris,

Try,
Dim intFileNum As Integer
Dim strBuffer As String
Dim lngBufferSize As Long

lngBufferSize = FileLen(strFileName$)
strBuffer = Space(lngBufferSize)     ' Create a Buffer to hold the information.

intFileNum = FreeFile(1)    ' Numbers 256 – 511
Open strFileName$ For Binary Access Read As #intFileNum
    Get #intFileNum, 1, strBuffer  'The easy way!
Close #intFileNum

text2.text= strBuffer

Hope this helps
ED
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2718057
try this:

Private Sub Form_Load()
    Dim A As String
    Open "c:\path\filename" For Input As 1
    A = Input(LOF(1), 1)
    Close 1
    Text1.Text = A
End Sub
0
 
LVL 2

Author Comment

by:ChrisK
ID: 2718118
Ed, your example works quite fast.  The only problem is nothing is showing up in the text box.  This isn't the biggest problem in the world since I can just work with it from the variable you stored it in, but what do you suppose the problem is?
0
 
LVL 2

Author Comment

by:ChrisK
ID: 2718124
Azra, your example doesn't work for me.  Gets an error on the A=Input(LOF(1),1) line.
0
 
LVL 2

Expert Comment

by:Ber
ID: 2718126
This code does the same thing as above
Just said I'd answer anyway

Private Sub Form_Load()
Dim TestFile As String
Dim Text As String
Dim Buffer As String

TestFile = "c:\windows\desktop\pop.txt"

Buffer = ""
Open TestFile For Input As 1
Do While Not EOF(1) ' Loop until end of file.
    Line Input #1, Text ' Read line into variable.
    Buffer = Buffer + Text
Loop
    Text1.Text = Buffer
Close #1    ' Close file.
End Sub

This will do the job - Tried and tested...
Good Luck
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2718128
no reason i can think of why it shouldnt
0
 
LVL 27

Expert Comment

by:Ark
ID: 2718135
Hi ChrisK
Here is your problem:
Text2.Text = Text2.Text & Asc(Dummy) & ","
ie your program read Text2.text every loop. And second - why you need ASC? You can read bytes from file directly. Try this:
Dim s As String, bDummy as Byte
Open tmpfile For Binary As #1
s = Space$(LOF(1) * 2)' if you want add comma after every byte, you'll need double space
For AC = 1 To LOF(1)
    Get #1, AC, bDummy
    Mid$(s, AC*2-1, 2) = cStr(bDummy) & ","
Next AC
Close #1
BTW, if you need HexDumper, your code should be more complex with leading Address strinf, vbCrLf after every 16 byte plus Hex address  at the begin of every string and "|" instead of comma <smile>
Cheers
0
 
LVL 2

Author Comment

by:ChrisK
ID: 2718141
Ark, thanks for the input but I already knew the method I was using sucked :P  I used asc with comma delimitation because I needed to count over to a specific byte in the file to work with it...I know there are other ways of doing this now.  At the time I wrote it, I didn't.
0
 
LVL 27

Expert Comment

by:Ark
ID: 2718173
ChrisK,
My 'BTW' comment is a joke. The main reason of your slow code is every loop reading Text2 content. Initialization of string (s = Space$(LOF(1) * 2)) and using Mid$ is about 10 times faster (for my P166MMX + 32M it's noticeably <smile>)
Cheers
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 28

Expert Comment

by:AzraSound
ID: 2718179
Ark, any ideas why my code doesnt work for him?
0
 
LVL 2

Author Comment

by:ChrisK
ID: 2718198
Azra, your input line gets "Input past the end of file" error.  
0
 
LVL 2

Author Comment

by:ChrisK
ID: 2718210
Perhaps it should be noted the files I'm reading are not plain text files, they are binary files that can contain the primary and extended character sets.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2718223
oh thats why my code is for text type files
0
 
LVL 27

Expert Comment

by:Ark
ID: 2718224
Azra,
It's no problem with error and your code is OK as many other, but, as I understand, ChrisK need to read file byte by byte to separate bytes with commas. So (s)he need a loop. And as I wrote before, the best way to speed up this code is using Mid$ statement instead of reading all string (which can be up to 2M length) every loop.
ChrisK, do you need all file? If you need only one byte, look at Seek statement.
Cheers
0
 
LVL 1

Expert Comment

by:Ask_ED
ID: 2718232
Hi Again,

The reason why my example above doesn't display in the textbox is that based on your comment at 4/14/2000 5:45PM the file is not plain text.

Most likely there is a CHR$(0) ( a null char) within the first few character and VB will truncate the display of the string at that point.

If you still want to display the information try replacing the

text2.text= strBuffer

with
text2.text= replace(strBuffer,chr$(0),"*")

and see what you get

Bye for Now
ED


0
 
LVL 27

Expert Comment

by:Ark
ID: 2718235
ChrisK,
my code above is exactly for binary files
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2718362
ChrisK:
Rather than guess on the file type.. can you tell us what it is? (ie.. the three letter file extension).

If this is a formatted data file.. there is definitely more work that has to be done. If it is a comma or tab delimited file, depending on the file size, there are other approaches that can be taken.. (ie. Memory map it and then parse). Not knowing what kind of data or the size of the file, you really have us at a loss.. <smile>.
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2718368
Ark:
Not if he is trying to load a Picture into a Textbox.. its not.. LOL and a <wink>.
0
 
LVL 2

Author Comment

by:ChrisK
ID: 2718446
Ed's code works for what I need to do.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now