reading specific line from a text file in vb 6
hi guys
have a look at this code
Code:
Open "C:\Score.txt" For Input As #1
Text1.Text = Input$(LOF(1), #1)
lblX.Caption = Text1.Text
lblO.Caption = Text1.Text
the score text file contains the following
when i run the program, and load the file, the lblX and the lblO
both display everything in the text file
but i would like the lblX to display the first line in the text file ie: 2
and the lblO to display the second line in the text file ie: 3
so how can i do this?
Re: reading specific line from a text file in vb 6
Only two lines in the text file? or only the 1st two lines needed? Then try this instead
Code:
Dim strLine As String
Open "C:\Score.txt" For Input As #1
Line Input #1, strLine ' read one line at a time vs entire file
lblX.Caption = strLine
Line Input #1, strLine
lblO.Caption = strLine
Close #1
Re: reading specific line from a text file in vb 6
only two lines in the text file
ps: i need to incorporate the LOF function. dont ask why, just part of my project.
Re: reading specific line from a text file in vb 6
Quote:
Originally Posted by kashhash
only two lines in the text file
ps: i need to incorporate the LOF function. dont ask why, just part of my project.
If you need to cache the LOF, then cache it, but you don't need to use it for the purpose you are describing. If I am misunderstanding, please provide more specific details
Re: reading specific line from a text file in vb 6
ok, i will leave the LOF file in the program but we wont use it in this example.
Code:
Dim strLine As String
Open "C:\Score.txt" For Input As #1
Line Input #1, strLine ' read one line at a time vs entire file
lblX.Caption = strLine
Line Input #1, strLine
lblO.Caption = strLine
Close #1
however this doesnt seem to be working :(
it says "input past end of file"
Re: reading specific line from a text file in vb 6
Is this homework? And what is not working? Details and/or error descriptions and what lines those errors may be occuring on.
Re: reading specific line from a text file in vb 6
Code:
Line Input #1, strLine ' read one line at a time vs entire file
error message [debug]: "input past end of file"
Re: reading specific line from a text file in vb 6
Need to see your routine now. That error generally means you already read the data once completely and the file was not closed or that the file is empty.
Re: reading specific line from a text file in vb 6
hi,
it works now but unfortunately the source of the error came from this line
Code:
Text1.Text = Input$(LOF(1), #1)
so i had to cut this line out
but this is the line i needed lol.
hmmm..
Re: reading specific line from a text file in vb 6
The code:
Line Input.....reads to the CR/LF...normally at the end of line in an ASCII file (see link below)
Input...reads to the next comma ','
Maybe the error is trying to read past the end of line or end of file (EOF)
Try testing the EOF first...If EOF(1) then.....
Input, test eof...if not then process
Re: reading specific line from a text file in vb 6
So is this homework? If not, why must you use the LOF function?
If this is homework, that is ok too, we may not write all the code but can tell you how to do what you are trying to do
Re: reading specific line from a text file in vb 6
copy your lines in alistbox using:
Function get_msg(usfile As String)
Dim sFile$, sText$, arLines() As String
Dim I%
sFile = usfile
Open sFile For Input As #1
sText = Input(LOF(1), #1)
Close #1
arLines = Split(sText, vbNewLine)
For I = 0 To UBound(arLines)
list1.AddItem arLines(I)
Next I
Close #1
'---------------------------------------------
End Function
then you can read the line you want from the listbox using
list1.list (line_number)
Re: reading specific line from a text file in vb 6
Try this:
VB Code:
Private Sub Form_Load()
Text1.MultiLine = True
Open "C:\Score.txt" For Input As #1
Text1.Text = Input$(LOF(1), #1)
lblX.Caption = udf_ReadLine(Text1.Text, 1) ' read line #1
lblO.Caption = udf_ReadLine(Text1.Text, 2) ' read line #2
Close #1
End Sub
Private Function udf_ReadLine(ByVal sDataText As String, ByVal nLineNum As Long) As String
Dim sText As String, nI As Long, nJ As Long, sTemp As String
On Error GoTo ErrHandler
sText = ""
nI = 1
nJ = 1
sTemp = ""
While (nI <= Len(sDataText))
Select Case Mid(sDataText, nI, 1)
Case vbCr
If (nJ = nLineNum) Then
sText = sTemp
End If
Case vbLf
nJ = nJ + 1
sTemp = ""
Case Else
sTemp = sTemp & Mid(sDataText, nI, 1)
End Select
nI = nI + 1
Wend
If (nJ = nLineNum) Then
sText = sTemp
End If
udf_ReadLine = sText
Exit Function
ErrHandler:
udf_ReadLine = ""
End Function
PS: I just added a function to read line from a string, and you can keep using the LOF function as you wish, also all of the concept from your original code.
good luck ;)
Re: reading specific line from a text file in vb 6
Re: reading specific line from a text file in vb 6
If your problem is solved, then mark the thread as RESOLVED from the THREAD TOOLS
-Best wishes to all:thumb:
Re: reading specific line from a text file in vb 6
I have to wonder what happened between April 7th and yesterday.
Amazing how mummies can sometimes emerge suddenly from their tombs. :eek:
Re: reading specific line from a text file in vb 6
Re: reading specific line from a text file in vb 6
Quote:
Originally Posted by akhileshbc
Doc, is that to me???
No, to either the OP or Batori. This post originally went off the screen last April. Apparently it went into some sort of hibernation. ;)
Re: reading specific line from a text file in vb 6
Oh i thought u r saying that to me because i was offline for some weeks...:)...
Gud nite:)