?
Solved

VB5: Fast Text File Loading

Posted on 2000-04-14
19
Medium Priority
?
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 4
  • +3
19 Comments
 
LVL 1

Accepted Solution

by:
Ask_ED earned 200 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 28

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 28

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
 
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 28

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 28

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

765 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