Results 1 to 5 of 5

Thread: How can i read only one line or specific line of a text file

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Location
    youngstown, oh
    Posts
    11
    How can i read only one line or specific line of a text file?

  2. #2
    New Member
    Join Date
    Aug 2000
    Posts
    10
    Public Sub ReadDetails(IsFilename As String)
    ' read the text from file

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set tem = fso.OpenTextFile(IsFilename)



    Do Until am = "end" Or am = "End" Or am = "END"

    am = tem.readline

    Loop

    end Sub

    This will read one line at a time, and in this spesific routine, it will stop when it hits the word "END" or "end" or "End". IsFilename = full path and filename.

    hope this helps.
    martin

  3. #3
    Guest
    Replace the bold number with whatever line number you want to get.

    Code:
    Private Sub Command1_Click()
    
        Dim strLine As String
        Dim iLine As Integer
        iLine = 0
        iFile = FreeFile
        Open "C:\MyText.txt" For Input As iFile
            Do While Not EOF(iFile)
            Line Input #iFile, strLine$
                iLine = iLine + 1
            If iLine = 50 Then MsgBox strLine$: Exit Sub
            Loop
        Close #iFile
        
    End Sub

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Location
    youngstown, oh
    Posts
    11
    what i really need is to get a specific line in a text file and let it equal a varible. example: i need line 5 to go into CNsme snf line 10 to go into gender ( cname and gender being varibles)

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Here's what you can do, open in binary and read the whole file into a buffer, then split it into lines and read any line into any variable anytime:
    Code:
    Dim buffer as string, lines() as string
    Open File for binary as 1
      buffer=space(lof(1))
      get#1,,buffer
      lines=split(buffer,vbcrlf)
      CNsme = lines(4)
      gender = lines(9)
    close 1
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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