Home > Archive > Visual Basic > November 2004 > ReadLine Method giving problems
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
ReadLine Method giving problems
|
|
|
| Hi,
I am getting a problem using the following piece of code:
Dim strfile As Object
Set strfile = FSO.OpenTextFile(stFilePath, 1)
Do While Not strfile.AtEndOfStream
strtext = strfile.ReadLine
Loop
I am using it to read text from a file.
Whenever the character count of the line goes in excess of 130, only the
131st character onwards are displayed. Any pointers?
CD
| |
| Jeff Johnson [MVP: VB] 2004-11-24, 3:55 pm |
|
"CD" <CD@discussions.microsoft.com> wrote in message
news:5A766BD6-BE53-45D6-92C4-106278C53174@microsoft.com...
> Whenever the character count of the line goes in excess of 130, only the
> 131st character onwards are displayed. Any pointers?
Displayed by what? How is strtext declared? And--we all know this is
coming--why are you using the FSO in the first place?
| |
|
| the strtext variable has been declared as a string and this variable while
parsing the entire file starts reading certain lines which have an excess of
130 characters from the 131st position. Ths File system Object has been used
to enable us to read all kinds of text files. Any pointers to an alternative.
Thanks
CD
| |
| Rick Rothstein 2004-11-25, 3:55 am |
| > the strtext variable has been declared as a string and this variable
while
> parsing the entire file starts reading certain lines which have an
excess of
> 130 characters from the 131st position. Ths File system Object has
been used
> to enable us to read all kinds of text files. Any pointers to an
alternative.
You can open and read a file in line by line like this...
Dim FF As Long
Dim LineOfText As String
FF = FreeFile
Open "c:\SomeDirectory\YourFileName.txt" For Input As #FF
Do While Not EOF(FF)
Line Input #FF, LineOfText
' Do whatever you want to with the
' currently read-in line from the file here
Loop
Close #FF
You can also read the entire file into a String variable using this
code...
Dim FileNum As Integer
Dim TotalFile As String
FileNum = FreeFile
' Reads the entire file into memory all at once
Open "c:\SomeDirectory\YourFileName.txt" For Binary As #FileNum
TotalFile = Space(LOF(FileNum))
Get #FileNum, , TotalFile
Text1.Text = TotalFile
Close #FileNum
' At this point, the entire file, including all newline character
' sequences, is contained in the TotalFile variable
Rick - MVP
| |
| StephenMcC 2004-11-25, 3:55 pm |
| What the benefit of using the FreeFile method rather then the FSO, Speed,
OverHeads?? Can the FreeFile be used in VBScript?
"Rick Rothstein" wrote:
> while
> excess of
> been used
> alternative.
>
> You can open and read a file in line by line like this...
>
> Dim FF As Long
> Dim LineOfText As String
> FF = FreeFile
> Open "c:\SomeDirectory\YourFileName.txt" For Input As #FF
> Do While Not EOF(FF)
> Line Input #FF, LineOfText
> ' Do whatever you want to with the
> ' currently read-in line from the file here
> Loop
> Close #FF
>
> You can also read the entire file into a String variable using this
> code...
>
> Dim FileNum As Integer
> Dim TotalFile As String
> FileNum = FreeFile
> ' Reads the entire file into memory all at once
> Open "c:\SomeDirectory\YourFileName.txt" For Binary As #FileNum
> TotalFile = Space(LOF(FileNum))
> Get #FileNum, , TotalFile
> Text1.Text = TotalFile
> Close #FileNum
> ' At this point, the entire file, including all newline character
> ' sequences, is contained in the TotalFile variable
>
> Rick - MVP
>
>
| |
| Rick Rothstein 2004-11-25, 3:55 pm |
| > What the benefit of using the FreeFile method rather then the
> FSO, Speed, OverHeads?? Can the FreeFile be used in VBScript?
FreeFile is not a method to be used in place of FSO, the Open statement
is. The Open statement needs to create a numbered channel for
communication (in case you have more than one channel open at a time);
the FreeFile function simply guarantees that it will give you a number
that is not currently being used by any other Open statement's channel.
Regarding the FSO, the following is usually what is posted in reply to
any suggestions to use it.
"You should consider discarding that as an option
because it is slow, adds overhead, requires an
extra DLL in distribution, can exist in multiple
non-compatible versions on user systems, can
be disabled on a computer as part of anti-virus
security measures and offers nothing that can't
be done with plain VB statements and/or a couple
of API calls."
Remember, we are talking about the classic forms of compiled VB (such as
versions 5 or 6) and not scripted editions such as VBScript.
As for your question about using FreeFile in VBScript... I don't know, I
don't program in VBScript. As an aside, this newsgroup is not devoted to
questions about VBScript; its purpose is for questions about the classic
VB editions.
Rick - MVP
| |
| Jeff Johnson [MVP: VB] 2004-11-26, 3:55 am |
|
"StephenMcC" <StephenMcC@discussions.microsoft.com> wrote in message
news:D51D2AC3-9537-4CB8-8817-A4F6C71233B6@microsoft.com...
> Can the FreeFile be used in VBScript?
No. The FSO was developed for VBScript because that language didn't support
the native VB file access statements. This is one of the reasons we
recommend against its use in pure VB.
|
|
|
|
|