From: NadCixelsyd on 26 Apr 2010 15:59 I have VB 5.0 - I'm writing a utility that reads records from files. I use "line input" to read the records into a string variable. It works all well and good until I attempt to read a large (over 2mb) record that has no newline characters (CR or LF). Using a normal file with an average of 80 characters between CRLF, it can read a 10mb file with 125k lines in under one second on my computer. However, if there are no newline characters, the programs hangs on the "LINE INPUT" statement. If, for example, I try and read a 3mb file without CRLF, it takes about 15 seconds, which is not acceptable. Is there any way to limit the number of characters being read at one time (e.g. limit record size to 4096 characters, thence use a SEEK to position myself 4096 bytes further into the file).
From: Karl E. Peterson on 26 Apr 2010 16:19 NadCixelsyd wrote: > I have VB 5.0 - I'm writing a utility that reads records from files. > I use "line input" to read the records into a string variable. It > works all well and good until I attempt to read a large (over 2mb) > record that has no newline characters (CR or LF). > > Using a normal file with an average of 80 characters between CRLF, it > can read a 10mb file with 125k lines in under one second on my > computer. However, if there are no newline characters, the programs > hangs on the "LINE INPUT" statement. If, for example, I try and read > a 3mb file without CRLF, it takes about 15 seconds, which is not > acceptable. > > Is there any way to limit the number of characters being read at one > time (e.g. limit record size to 4096 characters, thence use a SEEK to > position myself 4096 bytes further into the file). No, not really. If your files are all this small, I'd recommend reading them in all at once, then using Split to break them into an array of lines. FileLines = Split(ReadFile(TheFile), vbCrLf) Public Function ReadFile(ByVal FileName As String) As String Dim hFile As Long On Error GoTo Hell hFile = FreeFile Open FileName For Binary As #hFile ReadFile = Space$(LOF(hFile)) Get #hFile, , ReadFile Close #hFile Hell: End Function Or something similar, if that's not precisely appropriate. -- ..NET: It's About Trust! http://vfred.mvps.org
From: GS on 26 Apr 2010 17:34 It happens that NadCixelsyd formulated : > I have VB 5.0 - I'm writing a utility that reads records from files. > I use "line input" to read the records into a string variable. It > works all well and good until I attempt to read a large (over 2mb) > record that has no newline characters (CR or LF). > > Using a normal file with an average of 80 characters between CRLF, it > can read a 10mb file with 125k lines in under one second on my > computer. However, if there are no newline characters, the programs > hangs on the "LINE INPUT" statement. If, for example, I try and read > a 3mb file without CRLF, it takes about 15 seconds, which is not > acceptable. > > Is there any way to limit the number of characters being read at one > time (e.g. limit record size to 4096 characters, thence use a SEEK to > position myself 4096 bytes further into the file). It will hang whenever there's nothing on the line because <AFAIK> it returns NULL, which doesn't match your $tring variable. Karl's solution is a good one up to the point where you hit anything that is not a $tring. One thing you could do is use the Filter() function on the array after Split() so it removes any empty elements, leaving you with only valid $trings. If empty lines are required for spacing purposes you can optionally loop the array and replace the empty elements with an empty $tring. -Not to be confused with being NULL or EMPTY. If you're processing each line in a $tring variable then an empty $tring matches type, whereas NULL or EMPTY do not. Garry --
From: Mike Williams on 27 Apr 2010 01:18 "NadCixelsyd" <nadcixelsyd(a)aol.com> wrote in message news:9b3317bb-eced-4450-b372-0fd3ce59a75e(a)r9g2000vbk.googlegroups.com... > Is there any way to limit the number of characters being > read at one time (e.g. limit record size to 4096 characters, > thence use a SEEK to position myself 4096 bytes further > into the file). Have a look at the Input statement (as opposed to the Line Input statement) and also have a look at the Get statement. Mike
From: Dee Earley on 27 Apr 2010 05:02 On 26/04/2010 20:59, NadCixelsyd wrote: > I have VB 5.0 - I'm writing a utility that reads records from files. > I use "line input" to read the records into a string variable. It > works all well and good until I attempt to read a large (over 2mb) > record that has no newline characters (CR or LF). > > Using a normal file with an average of 80 characters between CRLF, it > can read a 10mb file with 125k lines in under one second on my > computer. However, if there are no newline characters, the programs > hangs on the "LINE INPUT" statement. If, for example, I try and read > a 3mb file without CRLF, it takes about 15 seconds, which is not > acceptable. > > Is there any way to limit the number of characters being read at one > time (e.g. limit record size to 4096 characters, thence use a SEEK to > position myself 4096 bytes further into the file). Further to the other answers, depending on what the files are and what you're doing with the data, reading as a byte array may be better: http://www.earlsoft.co.uk/tips/fileio.php -- Dee Earley (dee.earley(a)icode.co.uk) i-Catcher Development Team iCode Systems (Replies direct to my email address will be ignored. Please reply to the group.)
|
Pages: 1 Prev: How to send email with no outlook and SMTP server ? Next: Dynamic column |