Chr1s
04-18-2003, 05:29 PM
I've tried opening files using Open and Get but it takes around 2 minutes to open a 100 mb file. Is there any way using VB to open a file in binary mode that ranges from 100mbs to 200mbs within 5 seconds?
Fastest way of reading a fileChr1s 04-18-2003, 05:29 PM I've tried opening files using Open and Get but it takes around 2 minutes to open a 100 mb file. Is there any way using VB to open a file in binary mode that ranges from 100mbs to 200mbs within 5 seconds? Squirm 04-18-2003, 05:49 PM Show the code you are currently using to open the file and read in the data. Chr1s 04-18-2003, 06:17 PM Show the code you are currently using to open the file and read in the data. Public Function FileContents(filename As String) Dim ff As Integer Dim txt As String On Error GoTo Failed ff = OpenFile(filename, "BINARY") txt = Space(LOF(ff)) Get ff, , txt Close ff FileContents = txt Failed: End Function Public Function OpenFile(filename As String, mode As String) As Long Dim ff As Long On Error GoTo Failed ff = FreeFile OpenFile = ff Select Case UCase(mode) Case "INPUT" Open filename For Input As ff Case "OUTPUT" Open filename For Output As ff Case "APPEND" Open filename For Append As ff Case "RANDOM" Open filename For Random As ff Case "BINARY" Open filename For Binary As ff Case Else Open filename For Binary As ff End Select Exit Function Failed: OpenFile = 0 End Function It takes over 40 seconds to open a 100mb file using this method. OnErr0r 04-18-2003, 07:35 PM The reason its taking so long most likely has to do with the amount of memory you're using. Here is the code you posted: Public Function FileContents(filename As String) Dim ff As Integer Dim txt As String On Error Goto Failed ff = OpenFile(filename, "BINARY" ) txt = Space(LOF(ff)) ' allocate 200 megs Get ff, , txt ' read 100 megs from the HD and convert to unicode Close ff FileContents = txt ' allocate another 200 megs and copy original 200 megs into a variant Failed: End Function You can see right off you're allocating 400 megs and deallocating 200. Plus converting to unicode. You could do this all in 100 megs by passing a byte array to the function and allocating memory in it, plus there would be no unicode conversion. This might eventually need to happen if you need it in a string for some reason. A more efficient way to do this would be to read chunks (perhaps ~64K at a time) and process them one at a time. Chr1s 04-18-2003, 07:38 PM Could you show me a example code of how to read a file by chunks of 2048 bytes a time? OnErr0r 04-18-2003, 07:50 PM http://www.visualbasicforum.com/showpost.php?postid=343278&postcount=6 Chr1s 04-18-2003, 07:51 PM wow thanks a lot :D |
EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum