Re: Readfile for visual basic
- From: "Neil.." <guess@xxxxxxxxxxx>
- Date: Fri, 26 May 2006 22:08:52 +0100
"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..
.
- Follow-Ups:
- Re: Readfile for visual basic
- From: J French
- Re: Readfile for visual basic
- References:
- Readfile for visual basic
- From: john . ambou
- Re: Readfile for visual basic
- From: Rick Rothstein
- Re: Readfile for visual basic
- From: Neil..
- Re: Readfile for visual basic
- From: Rick Rothstein
- Readfile for visual basic
- Prev by Date: Re: fastest way to change case of string
- Next by Date: Re: VB6 Winsock action on Server
- Previous by thread: Re: Readfile for visual basic
- Next by thread: Re: Readfile for visual basic
- Index(es):
Relevant Pages
|