Home All Groups Group Topic Archive Search About
Author
19 Oct 2005 9:45 PM
Dave B
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.

Author
19 Oct 2005 9:45 PM
Rick Rothstein [MVP - Visual Basic]
> 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
Author
19 Oct 2005 10:15 PM
Dave B
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
>
>
Author
20 Oct 2005 8:42 AM
J French
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
>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?

Having looked at your code, I would suggest that you open the file in
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
Author
20 Oct 2005 4:26 PM
Rick Rothstein [MVP - Visual Basic]
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
news: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
Then Exit For
>         Next j
>
>         For j = 1 To 100
>             If intTableLoc(j, 0) = 0 Or intTableLoc(j, 1) = 0
Then Exit For
Show quoteHide quote
>             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
>
>
> "Rick Rothstein [MVP - Visual Basic]"
<rickNOSPAMnews@NOSPAMcomcast.net>
Show quoteHide quote
> 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
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
> >
> >
>
>