|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
text files & stringsI'm trying to pick tables out of a group of text files. I wrote code that
would load the text file into a string, use InStr to find the location of each <TABLE and </TABLE>, then use Mid to pick the data out. It works great .... except with larger files (>3MB) I get an out of memory error. How can I go through larger files without trying to load the whole text file into a string? I can't use Input because the files are not comma-delimited, and I don't think I can use Line Input because sometimes there isn't a line feed in the whole text file. Any ideas on how I can do this? I'm using Visual Basic 6. > I'm trying to pick tables out of a group of text files. I wrote error. How can Icode that > would load the text file into a string, use InStr to find the location of > each <TABLE and </TABLE>, then use Mid to pick the data out. It works great > ... except with larger files (>3MB) I get an out of memory > go through larger files without trying to load the whole text file into a> string? I can't use Input because the files are not comma-delimited, and I> don't think I can use Line Input because sometimes there isn't a You will need to show us your code... 3 Meg files are not tooline feed > in the whole text file. Any ideas on how I can do this? I'm using Visual > Basic 6. large for VB Strings to handle (I've had code that loaded up to 50 Megs of text and manipulated them quite successfully using slow functions like Split and Replace). Rick My system only has 256MB of RAM. In my search I saw others who had this
problem with different max size, depending on system RAM. They fixed it with a file stream. Is that for VB.Net or can you do that in VB6, too? Here's my code. The out of memory error happens at InStr: For i = 1 To 1000 Open strFileNames2(i) For Input As #1 intFileSize = FileLen(strFileNames2(i)) str10KText = Input(LOF(1), 1) Close #1 intTableLoc(0, 0) = 1 intTableLoc(0, 1) = 1 For j = 1 To 100 intTableLoc(j, 0) = InStr(intTableLoc(j - 1, 0), str10KText, "<TABLE", vbTextCompare) intTableLoc(j, 1) = InStr(intTableLoc(j - 1, 1), str10KText, "</TABLE>", vbTextCompare) If intTableLoc(j, 0) = 0 Or intTableLoc(j, 1) = 0 Then Exit For Next j For j = 1 To 100 If intTableLoc(j, 0) = 0 Or intTableLoc(j, 1) = 0 Then Exit For intStart = intTableLoc(j, 0) intLength = intTableLoc(j, 1) - intTableLoc(j, 0) strNewFileContents = strNewFileContents & Mid(str10KText, intStart, intLength) Next j Open strFileNames2(i) For Output As #2 Print #2, strNewFileContents Close #2 'clear array, what else need to be reset? For j = 1 To 100 intTableLoc(j, 0) = 0 intTableLoc(j, 1) = 0 Next j strNewFileContents = "" str10KText = "" Next i Show quoteHide quote "Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in message news:u2JJ0ZP1FHA.732@TK2MSFTNGP10.phx.gbl... > > I'm trying to pick tables out of a group of text files. I wrote > code that > > would load the text file into a string, use InStr to find the > location of > > each <TABLE and </TABLE>, then use Mid to pick the data out. It > works great > > ... except with larger files (>3MB) I get an out of memory > error. How can I > > go through larger files without trying to load the whole text > file into a > > string? I can't use Input because the files are not > comma-delimited, and I > > don't think I can use Line Input because sometimes there isn't a > line feed > > in the whole text file. Any ideas on how I can do this? I'm > using Visual > > Basic 6. > > You will need to show us your code... 3 Meg files are not too > large for VB Strings to handle (I've had code that loaded up to 50 > Megs of text and manipulated them quite successfully using slow > functions like Split and Replace). > > Rick > > On Wed, 19 Oct 2005 18:15:51 -0400, "Dave B"
<davidbarbe***@hotmail.com> wrote: >My system only has 256MB of RAM. In my search I saw others who had this Having looked at your code, I would suggest that you open the file in>problem with different max size, depending on system RAM. They fixed it >with a file stream. Is that for VB.Net or can you do that in VB6, too? Binary mode and read in blocks of say 100k Once you have found: <TABLE you can discard anything before and start looking for </TABLE> If you don't find it then keep reading in blocks and adding them to the string. As soon as you have found a complete <TABLE </TABLE then process it immediately and write it to the second file. That way you are not (hopefully) working with a lot of enormous strings. I also suggest that you look into: FreeFile - also that you don't have fixed loops eg: For I = 1 To 1000 Are you able to determine when the out of memory error is
occurring? In the Open block when you assign the file's text to str10Ktext? Or when you concatenate the text together in the middle loop using the following line? strNewFileContents = strNewFileContents & Mid(str10KText, intStart, intLength) Also, how large is the largest file you will have to deal with? Rick Show quoteHide quote "Dave B" <davidbarbe***@hotmail.com> wrote in message Then Exit Fornews:OjGnjlP1FHA.3084@TK2MSFTNGP09.phx.gbl... > My system only has 256MB of RAM. In my search I saw others who had this > problem with different max size, depending on system RAM. They fixed it > with a file stream. Is that for VB.Net or can you do that in VB6, too? > Here's my code. The out of memory error happens at InStr: > > > For i = 1 To 1000 > Open strFileNames2(i) For Input As #1 > intFileSize = FileLen(strFileNames2(i)) > str10KText = Input(LOF(1), 1) > Close #1 > > intTableLoc(0, 0) = 1 > intTableLoc(0, 1) = 1 > > For j = 1 To 100 > intTableLoc(j, 0) = InStr(intTableLoc(j - 1, 0), str10KText, > "<TABLE", vbTextCompare) > intTableLoc(j, 1) = InStr(intTableLoc(j - 1, 1), str10KText, > "</TABLE>", vbTextCompare) > If intTableLoc(j, 0) = 0 Or intTableLoc(j, 1) = 0 > Next j Then Exit For> > For j = 1 To 100 > If intTableLoc(j, 0) = 0 Or intTableLoc(j, 1) = 0 Show quoteHide quote > intStart = intTableLoc(j, 0) <rickNOSPAMnews@NOSPAMcomcast.net>> intLength = intTableLoc(j, 1) - intTableLoc(j, 0) > strNewFileContents = strNewFileContents & Mid(str10KText, > intStart, intLength) > Next j > > Open strFileNames2(i) For Output As #2 > Print #2, strNewFileContents > Close #2 > > 'clear array, what else need to be reset? > For j = 1 To 100 > intTableLoc(j, 0) = 0 > intTableLoc(j, 1) = 0 > Next j > strNewFileContents = "" > str10KText = "" > Next i > > > "Rick Rothstein [MVP - Visual Basic]" Show quoteHide quote > wrote in message news:u2JJ0ZP1FHA.732@TK2MSFTNGP10.phx.gbl... isn't a> > > I'm trying to pick tables out of a group of text files. I wrote > > code that > > > would load the text file into a string, use InStr to find the > > location of > > > each <TABLE and </TABLE>, then use Mid to pick the data out. It > > works great > > > ... except with larger files (>3MB) I get an out of memory > > error. How can I > > > go through larger files without trying to load the whole text > > file into a > > > string? I can't use Input because the files are not > > comma-delimited, and I > > > don't think I can use Line Input because sometimes there Show quoteHide quote > > line feed > > > in the whole text file. Any ideas on how I can do this? I'm > > using Visual > > > Basic 6. > > > > You will need to show us your code... 3 Meg files are not too > > large for VB Strings to handle (I've had code that loaded up to 50 > > Megs of text and manipulated them quite successfully using slow > > functions like Split and Replace). > > > > Rick > > > > > >
Using shell command to map drive can't get redirect result to file
Modal form stops code in another form INSERT INTO how to split numeric part from letters in string Shrink Wrap Iterative GetObject get list of file names in a directory Is any device connected to COM1 port InStr search counter Printing Blank lines for Top margin |
|||||||||||||||||||||||