• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

VB5: Fast Text File Loading

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
ChrisK
Asked:
ChrisK
  • 6
  • 4
  • 4
  • +3
1 Solution
 
Ask_EDCommented:
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
 
AzraSoundCommented:
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
 
ChrisKAuthor Commented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ChrisKAuthor Commented:
Azra, your example doesn't work for me.  Gets an error on the A=Input(LOF(1),1) line.
0
 
BerCommented:
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
 
AzraSoundCommented:
no reason i can think of why it shouldnt
0
 
ArkCommented:
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
 
ChrisKAuthor Commented:
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
 
ArkCommented:
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
 
AzraSoundCommented:
Ark, any ideas why my code doesnt work for him?
0
 
ChrisKAuthor Commented:
Azra, your input line gets "Input past the end of file" error.  
0
 
ChrisKAuthor Commented:
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
 
AzraSoundCommented:
oh thats why my code is for text type files
0
 
ArkCommented:
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
 
Ask_EDCommented:
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
 
ArkCommented:
ChrisK,
my code above is exactly for binary files
0
 
wsh2Commented:
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
 
wsh2Commented:
Ark:
Not if he is trying to load a Picture into a Textbox.. its not.. LOL and a <wink>.
0
 
ChrisKAuthor Commented:
Ed's code works for what I need to do.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 6
  • 4
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now