|
-
Apr 5th, 2008, 11:01 AM
#1
Thread Starter
Junior Member
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?
-
Apr 5th, 2008, 11:08 AM
#2
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
-
Apr 5th, 2008, 11:11 AM
#3
Thread Starter
Junior Member
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.
-
Apr 5th, 2008, 11:15 AM
#4
Re: reading specific line from a text file in vb 6
 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
-
Apr 5th, 2008, 11:16 AM
#5
Thread Starter
Junior Member
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"
-
Apr 5th, 2008, 11:17 AM
#6
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.
-
Apr 5th, 2008, 11:21 AM
#7
Thread Starter
Junior Member
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"
-
Apr 5th, 2008, 11:27 AM
#8
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.
-
Apr 5th, 2008, 11:32 AM
#9
Thread Starter
Junior Member
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..
-
Apr 5th, 2008, 11:39 AM
#10
Fanatic Member
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
Alpha Micro: Alpha Basic, AS400 V5r2, EDI (Trusted Link/ Inovis.com),Access AS/400 via VB6, Qbasic for data conversions. A mix of Hardware too. ASCII Table , New Number to Words/66 digits , AS/400(v5r2) VB6 Viewer/Ask for code(ODBC) ^ What Is Transferring? , Check your Ports #Perfect Passwords , *Slide Bar Example , Logoff, Restart, Shut-Down PC *Keep Form On Top , Opaque Form ^ Create Objects at Run Time @ Check Key Caps Locks # GetTickCount(System Up Time) * Convert text to Excel & Collected Icons + Resize: Form/Text box ^ PC GateWay via Shell $ Drag & Drop Game ! PopUpMenu *Print File/no Open# Timer on Mult Forms ~ Splash & Mult Forms & Lots of Comments + Random/Timer/Guess * Dec >Hex >Oct >Bin % Get MAC (NIC) < saving to Registry > Wookiee Cookies \ BackUpDisk / World Conection SpeedTest $ Glossary Commonly Used Terms # phonetic list @ Detailed Computer Scan
When posting Code, Use tags.. [CODE] *Your Code* [/CODE]
-
Apr 5th, 2008, 11:41 AM
#11
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
-
Apr 5th, 2008, 10:20 PM
#12
Addicted Member
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)
-
Apr 7th, 2008, 01:35 AM
#13
Lively Member
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
-
Sep 28th, 2008, 05:41 PM
#14
Hyperactive Member
Re: reading specific line from a text file in vb 6
works..thanks
-
Sep 29th, 2008, 07:11 AM
#15
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
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Sep 29th, 2008, 10:07 AM
#16
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.
-
Sep 29th, 2008, 12:11 PM
#17
Re: reading specific line from a text file in vb 6
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Oct 1st, 2008, 10:41 AM
#18
Re: reading specific line from a text file in vb 6
 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.
-
Oct 1st, 2008, 10:51 AM
#19
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
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|