Re: Readfile for visual basic

Tech-Archive recommends: Fix windows errors by optimizing your registry




"Rick Rothstein" <rickNOSPAMnews@xxxxxxxxxxxxxxxxx> wrote in message
news:%23FyWNWQgGHA.1520@xxxxxxxxxxxxxxxxxxxxxxx
I'm not familiar with C++ so I don't know what Readfile does for sure,
but if you are looking to read in an entire text file into a String
variable, the following code will do that...

Dim FileNum As Long
Dim TotalFile As String
' Let VB generate the file channel number
FileNum = FreeFile
' Reads the whole file into memory all at once
Open "c:\sample\path\FileName.txt" For Binary As #FileNum
TotalFile = Space(LOF(FileNum))
Get #FileNum, , TotalFile
Close #FileNum

At this point, the TotalFile variable contains the entire text file.

Rick

Could you answer a couple of questions about your solution as I believe
that it will not always work?
You are declaring TotalFile as string, what happens if one of the
characters is the "end-of-string" (chr$(29) I think)?. Does this not
cause a problem?
Is there not a limit to the max size that can be stored as a string (I
thought about 2 Mb).

I would love to be proved wrong on each point.

The End-Of-File marker (ASCII 26), what I think you meant instead of
"end-of-string", will not cause any problems with the method I've
outlined. Also, while a file of this size would be completely impractical
to read into memory because of the contiguous memory space that would need
to be found, the method I posted will work to a theoretical limit of a
string (about 2 billion characters).

Rick


Thanks for the reply.
I'll give it a try when I have chance as I want to load a 6 Mb binary file
and send it via a Winsock-tcpip type link (I don't need compression) and
then recreate it at the other end. I assumed that the End-of-File marker
which could occur in the string would "corrupt" the string.
Time for an experiment I think!

Neil..



.



Relevant Pages

  • Re: Array Reference
    ... Public TotalFile As String ... Dim FileNum As Integer ... Dim TotalFile As String ...
    (microsoft.public.vb.general.discussion)
  • Re: Readfile for visual basic
    ... Dim FileNum As Long ... Dim TotalFile As String ...
    (microsoft.public.vb.general.discussion)
  • Re: How to read text-file into VB?
    ... Function ReadExcludeFile(sFileNameAndPath As String) ... Dim ChosenFile As String ... Dim FileNum As Long ... Dim TotalFile() As Byte ...
    (microsoft.public.vb.general.discussion)
  • Re: Excel in VB 6.0
    ... >> You can load the entire file into a String variable like this... ... >> Dim FileNum As Long ... >> Dim TotalFile As String ... >> Fields = Split, Delimiter) ...
    (microsoft.public.vb.general.discussion)
  • Re: Readfile for visual basic
    ... Dim FileNum As Long ... Dim TotalFile As String ...
    (microsoft.public.vb.general.discussion)