For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > February 2006 > Reading/writing specific lines of text file









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 Reading/writing specific lines of text file
Yeti

2006-02-26, 6:55 pm

Hi all, I am fairly new to VB (but do have a decent programming background
from college as an EE)..and I was wondering how to read specific lines of a
text file into VB 6.0 (we only use 6.0 at workk for various reasons). I
can't seem to find any working code to do this online. Also, on the opposite
end, is there a way to print to specific lines of a text file? Any code
would be exceptionally helpful. Many thanks.....
Jim Edgar

2006-02-26, 6:55 pm


"Yeti" <Yeti@discussions.microsoft.com> wrote in message
news:7343B31B-4E64-43C7-A803-E6CF83506742@microsoft.com...
> Hi all, I am fairly new to VB (but do have a decent programming background
> from college as an EE)..and I was wondering how to read specific lines of

a
> text file into VB 6.0 (we only use 6.0 at workk for various reasons). I
> can't seem to find any working code to do this online. Also, on the

opposite
> end, is there a way to print to specific lines of a text file? Any code
> would be exceptionally helpful. Many thanks.....


Try stepping thru this quick example to get one way to do it.

Function GetLineOfText(ByVal strFileName As String, _
ByVal nLineNumber As Long) As String
Dim iFileNum As Integer
Dim str As String
Dim arr As Variant
On Error GoTo GetLineOfText_Error
' Open the file to be parsed.
iFileNum = FreeFile
Open strFileName For Binary As #iFileNum
' Get the file information.
str = Space(LOF(iFileNum))
Get #iFileNum, , str
' Done with the file so close it.
Close #iFileNum
' Split the file into an array and get the nLineNumber element
' Since Split returns a zero based array then we need to
' substract 1 from the line number.
GetLineOfText = Split(str, vbCrLf)(nLineNumber - 1)
Exit Function
GetLineOfText_Error:
GetLineOfText = ""
End Function

Jim Edgar


Yeti

2006-02-27, 6:56 pm

Thanks Jim...worked beautiful...

"Jim Edgar" wrote:

>
> "Yeti" <Yeti@discussions.microsoft.com> wrote in message
> news:7343B31B-4E64-43C7-A803-E6CF83506742@microsoft.com...
> a
> opposite
>
> Try stepping thru this quick example to get one way to do it.
>
> Function GetLineOfText(ByVal strFileName As String, _
> ByVal nLineNumber As Long) As String
> Dim iFileNum As Integer
> Dim str As String
> Dim arr As Variant
> On Error GoTo GetLineOfText_Error
> ' Open the file to be parsed.
> iFileNum = FreeFile
> Open strFileName For Binary As #iFileNum
> ' Get the file information.
> str = Space(LOF(iFileNum))
> Get #iFileNum, , str
> ' Done with the file so close it.
> Close #iFileNum
> ' Split the file into an array and get the nLineNumber element
> ' Since Split returns a zero based array then we need to
> ' substract 1 from the line number.
> GetLineOfText = Split(str, vbCrLf)(nLineNumber - 1)
> Exit Function
> GetLineOfText_Error:
> GetLineOfText = ""
> End Function
>
> Jim Edgar
>
>
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com