Results 1 to 19 of 19

Thread: reading specific line from a text file in vb 6

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    27

    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

    Code:
    2
    3
    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?

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    27

    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.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    27

    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"

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    27

    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"

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    27

    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..

  10. #10
    Fanatic Member sessi4ml's Avatar
    Join Date
    Nov 2006
    Location
    Near San Francisco
    Posts
    958

    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

  11. #11
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  12. #12
    Addicted Member
    Join Date
    Apr 2008
    Posts
    198

    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)

  13. #13
    Lively Member
    Join Date
    Nov 2007
    Posts
    98

    Re: reading specific line from a text file in vb 6

    Try this:

    VB Code:
    1. Private Sub Form_Load()
    2.    Text1.MultiLine = True
    3.    Open "C:\Score.txt" For Input As #1
    4.    Text1.Text = Input$(LOF(1), #1)
    5.  
    6.    lblX.Caption = udf_ReadLine(Text1.Text, 1)   ' read line #1
    7.    lblO.Caption = udf_ReadLine(Text1.Text, 2)   ' read line #2
    8.  
    9.    Close #1
    10. End Sub
    11.  
    12. Private Function udf_ReadLine(ByVal sDataText As String, ByVal nLineNum As Long) As String
    13.     Dim sText As String, nI As Long, nJ As Long, sTemp As String
    14.  
    15.     On Error GoTo ErrHandler
    16.  
    17.     sText = ""
    18.     nI = 1
    19.     nJ = 1
    20.     sTemp = ""
    21.     While (nI <= Len(sDataText))
    22.         Select Case Mid(sDataText, nI, 1)
    23.             Case vbCr
    24.                 If (nJ = nLineNum) Then
    25.                     sText = sTemp
    26.                 End If
    27.             Case vbLf
    28.                 nJ = nJ + 1
    29.                 sTemp = ""
    30.             Case Else
    31.                 sTemp = sTemp & Mid(sDataText, nI, 1)
    32.         End Select
    33.         nI = nI + 1
    34.     Wend
    35.     If (nJ = nLineNum) Then
    36.         sText = sTemp
    37.     End If
    38.     udf_ReadLine = sText
    39.  
    40.     Exit Function
    41.  
    42. ErrHandler:
    43.     udf_ReadLine = ""
    44. 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

  14. #14
    Hyperactive Member
    Join Date
    Aug 2008
    Posts
    353

    Re: reading specific line from a text file in vb 6

    works..thanks

  15. #15
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    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,...

  16. #16
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    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.
    Doctor Ed

  17. #17
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: reading specific line from a text file in vb 6

    Doc, is that to me???

    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,...

  18. #18
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    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.
    Doctor Ed

  19. #19
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    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
  •  



Click Here to Expand Forum to Full Width