[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.vb.general.discussion

I want to allocate a very large string

NadCixelsyd

1/11/2012 6:48:00 PM

I want to allocate a very-long string so that I can read data into it
with a binary GET. Now I could...

myString$ = Space(VeryBigNumber&)
Open myFile$ for Binary as #23
GET #23, , myString$

First of all, I don't care about the contents of myString so there is
no need to initialize it with spaces.
Second: Space() and String() return a variant. I assume that VB6
will then need to allocate the huge Variant as a string variable, and
then copy the spaces into it before releasing the temporary Variant
returned by Space().

Since I can't specify a length parameter on the GET statement, is
there a way to simply allocate a string variable?
19 Answers

Karl E. Peterson

1/11/2012 7:19:00 PM

0

After serious thinking NadCixelsyd wrote :
> I want to allocate a very-long string so that I can read data into it
> with a binary GET. Now I could...
>
> myString$ = Space(VeryBigNumber&)
> Open myFile$ for Binary as #23
> GET #23, , myString$
>
> First of all, I don't care about the contents of myString so there is
> no need to initialize it with spaces.
> Second: Space() and String() return a variant. I assume that VB6
> will then need to allocate the huge Variant as a string variable, and
> then copy the spaces into it before releasing the temporary Variant
> returned by Space().
>
> Since I can't specify a length parameter on the GET statement, is
> there a way to simply allocate a string variable?

Without knowing the whole story, I'd start by suggesting you think
about using a Byte array rather than a String. That addresses several
concerns of yours, both stated and unstated.

--
..NET: It's About Trust!
http://vfre...


Jim Mack

1/11/2012 7:38:00 PM

0

> I want to allocate a very-long string so that I can read data into it
> with a binary GET. Now I could...
>
> myString$ = Space(VeryBigNumber&)
> Open myFile$ for Binary as #23
> GET #23, , myString$
>
> First of all, I don't care about the contents of myString so there is
> no need to initialize it with spaces.
> Second: Space() and String() return a variant. I assume that VB6
> will then need to allocate the huge Variant as a string variable, and
> then copy the spaces into it before releasing the temporary Variant
> returned by Space().
>
> Since I can't specify a length parameter on the GET statement, is
> there a way to simply allocate a string variable?

Yes. Use Space$() instead of Space(). (-:

And Space$ is faster than String$ -- not much, but some.

Depending on how you'll use the data, Karl's suggestion of a byte array
might be optimal. But if you need it as a string, I don't think you
gain anything by avoiding what you show above. One way or another, VB
will read the file as binary data, then convert it to a string.

--
Jim


BeeJ

1/11/2012 11:01:00 PM

0

Other solutions might present if you stated what you are going to do
once this string is in memory. You could bring in chunks and work on
those individually.

Also, how big is this string typically?


Karl E. Peterson

1/12/2012 12:09:00 AM

0

on 1/11/2012, BeeJ supposed :
> Other solutions might present if you stated what you are going to do once
> this string is in memory. You could bring in chunks and work on those
> individually.

This might have been a clue...

"NadCixelsyd" <nadcixelsyd@aol.com> wrote in message
news:9307e65f-dfea-455c-99c2-1998dc7a7cc0@x7g2000yqb.googlegroups.com...
> Are there publicly available functions wherein floating-point
> arithmetic can be performed on numbers with (for example) 80 digits of
> precision? I assume these functions would have their own internal
> format, stored, most likely in a VB string. I would need + - * / ^
> sqr exp ln sin cos atn, etc. It would be best if these functions were
> written in VB itself.

(One of the advantages of reading months worth of posts one lazy
afternoon. <g>)

--
..NET: It's About Trust!
http://vfre...


Karl E. Peterson

1/12/2012 12:13:00 AM

0

Jim Mack used his keyboard to write :
>> I want to allocate a very-long string so that I can read data into it
>> with a binary GET. Now I could...
>>
>> myString$ = Space(VeryBigNumber&)
>> Open myFile$ for Binary as #23
>> GET #23, , myString$
>>
>> First of all, I don't care about the contents of myString so there is
>> no need to initialize it with spaces.
>> Second: Space() and String() return a variant. I assume that VB6
>> will then need to allocate the huge Variant as a string variable, and
>> then copy the spaces into it before releasing the temporary Variant
>> returned by Space().
>>
>> Since I can't specify a length parameter on the GET statement, is
>> there a way to simply allocate a string variable?
>
> Yes. Use Space$() instead of Space(). (-:
>
> And Space$ is faster than String$ -- not much, but some.
>
> Depending on how you'll use the data, Karl's suggestion of a byte array might
> be optimal. But if you need it as a string, I don't think you gain anything
> by avoiding what you show above. One way or another, VB will read the file as
> binary data, then convert it to a string.

I confess I'd seen another post by the same person, and it was about
doing very high-precision math. If that's what this binary data is,
I'm not sure I'd be trusting it to the vagaries of Unimess. Okay, I'm
pretty damn sure I wouldn't. <g> I agree, though, that *if* it really
is just great gobs of text, there's little advantage in the Byte array
approach.

--
..NET: It's About Trust!
http://vfre...


NadCixelsyd

1/12/2012 2:58:00 AM

0

On Jan 11, 6:01 pm, BeeJ <nos...@spamnot.com> wrote:
> Other solutions might present if you stated what you are going to do
> once this string is in memory.  You could bring in chunks and work on
> those individually.
>
> Also, how big is this string typically?

I'm processing text files. Under certain circumstances, I want to
place the entire file in the clipboard to pass to another
application. While rare, it's possible for the files to be several
megabytes. I do the following

Find the size of the text file; allocate a string of that size; open
the text file; Read the entire file at once; place the date on the
clipboard.


TextRecord$ = Space$( FileLen( InputFileName$ ))
Open InputFileName$ for binary as #307
Get #307, , TextRecord$
Clipboard.Clear
Clipboard.SetText TextRecord$

While a trivial part of the exercise, it's the first statement (the
TextRecord$ allocation) that I want to do as quickly as possible.

GS

1/12/2012 4:42:00 AM

0

One way...

Public Sub ReadBinaryIntoString(ByRef szFullName As String, _
ByRef bytData() As Byte)
Dim iNum As Integer
iNum = FreeFile(): Open szFullName For Binary As #iNum
ReDim bytData(1 To LOF(iNum)): Get #iNum, 1, bytData
Close #iNum
End Sub


Another way...

Function GetTextFromFile(sFileName As String) As String
' Opens and reads the contents of a text file
Dim iNum As Integer, bOpen As Boolean, sz As String

On Error GoTo ErrHandler
iNum = FreeFile() 'Get the next file number
'Read the entire file
Open sFileName For Binary Access Read As #iNum
bOpen = True '//if we got here then file opened successfully
GetTextFromFile = Space$(LOF(iNum)): Get iNum, , GetTextFromFile

ErrHandler:
If bOpen Then Close #iNum
End Function '//GetTextFromFile()

HTH

--
Garry

Free usenet access at http://www.eternal-sep...
ClassicVB Users Regroup! comp.lang.basic.visual.misc


Karl E. Peterson

1/12/2012 6:18:00 PM

0

NadCixelsyd wrote on 1/11/2012 :
> On Jan 11, 6:01 pm, BeeJ <nos...@spamnot.com> wrote:
>> Other solutions might present if you stated what you are going to do
>> once this string is in memory.  You could bring in chunks and work on
>> those individually.
>>
>> Also, how big is this string typically?
>
> I'm processing text files. Under certain circumstances, I want to
> place the entire file in the clipboard to pass to another
> application. While rare, it's possible for the files to be several
> megabytes. I do the following
>
> Find the size of the text file; allocate a string of that size; open
> the text file; Read the entire file at once; place the date on the
> clipboard.
>
>
> TextRecord$ = Space$( FileLen( InputFileName$ ))
> Open InputFileName$ for binary as #307
> Get #307, , TextRecord$
> Clipboard.Clear
> Clipboard.SetText TextRecord$
>
> While a trivial part of the exercise, it's the first statement (the
> TextRecord$ allocation) that I want to do as quickly as possible.

Try timing this, then, versus that...

Private Declare Function SysAllocStringLen Lib "oleaut32" (ByVal
olestr As Long, ByVal bLen As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory"
(Destination As Any, Source As Any, ByVal length As Long)

Public Function ReadFileEx(ByVal FileName As String) As String
Dim hFile As Long
On Error GoTo Hell
' When speed is critical, this is ~15% faster than "Pure VB"
hFile = FreeFile
Open FileName For Binary As #hFile
CopyMemory ByVal VarPtr(ReadFileEx), SysAllocStringLen(0&,
LOF(hFile)), 4&
Get #hFile, , ReadFileEx
Close #hFile
Hell:
End Function

--
..NET: It's About Trust!
http://vfre...


GS

1/12/2012 7:58:00 PM

0

Ha, ha!!!
Karl, this is by far the biggest "make my day" post I've read in a long
time! Gotta' love it!

BTW, Happy New Year!
Regards

--
Garry

Free usenet access at http://www.eternal-sep...
ClassicVB Users Regroup! comp.lang.basic.visual.misc


Karl E. Peterson

1/12/2012 9:02:00 PM

0

GS has brought this to us :
> Ha, ha!!!
> Karl, this is by far the biggest "make my day" post I've read in a long time!
> Gotta' love it!
>
> BTW, Happy New Year!
> Regards

Wish I could recall the original source more precisely than "here." <g>
(Maybe Jim Mack?) It was definitely a nugget worth tucking away.

--
..NET: It's About Trust!
http://vfre...