Results 1 to 9 of 9

Thread: reading a certain line from a text file

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Hi,

    I know how to open a text file for input but I don't know how to do the following:

    I have a text file named data.txt. It is not delimited except for CRLFs at the end of each line.

    The start of each line has a number such as:

    907
    908
    909
    etc...

    What I need to be able to do is to read the complete line in to a variable that starts with 907.

    Any help would be appreciated..

    DAn

  2. #2
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349

    I don't know!!

    I am just putting this here cos I also need to know how to do this.

    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  3. #3
    Hyperactive Member dsy5's Avatar
    Join Date
    Jul 2000
    Location
    Lockport, NY
    Posts
    362
    I don't think it's possible to generate a variable at run time after you read it from the file.
    Why would you want to do that, anyway?
    Donald Sy - VB (ab)user

  4. #4
    Addicted Member
    Join Date
    Oct 2000
    Posts
    137

    Talking

    Maybe try someting like this:

    Private Sub Command1_Click()
    Dim tmpVar as String
    Open “pathname” For Input As #1
    Line Input #1, tmpVar

    If LOF(#1) = 193 Then
    Text1.text = tmpVar
    Else
    End Sub

    or something to that matter.

  5. #5
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    ? it's not that hard at all mateys!!!

    Code:
    Private Sub Form_Load()
    Dim filename As String, fn As Integer, tmp As String
    
    filename = "c:\MYFILE.txt"
    fn = FreeFile
    
    'Open the specified file
    Open filename For Input As #fn
    'Loop it while not end of file
    Do While Not (EOF(fn))
    'Get the next line
        Line Input #fn, tmp
        'strip the first 3 characters of it
        tmp = Left(tmp, 3)
        'Show it in a msgbox
        MsgBox tmp
    Loop
    'Close the specified file
    Close
    End Sub
    But this will only work if the number always consist of 3 numbers.
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  6. #6
    Guest
    Have a look at this code:

    Code:
        Dim TheStringArray(1000)
        Dim T As Integer
        Dim Pos As Integer
        
        Dim TheTestString(1) As String
        
        
        TheTestString(0) = "907 Hello this is line 907"
        TheTestString(1) = "104 Hello this is line onehundred and four"
        
        For T = 0 To UBound(TheTestString)
            ' Process the line
            Pos = Val(Left(TheTestString(T), InStr(TheTestString(T), " ")))
            TheStringArray(Pos) = TheTestString(T)
        Next T
    
        MsgBox TheStringArray(104)
        MsgBox TheStringArray(907)
    It ain't that hard, is it?

  7. #7
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    [b]RobIII :It ain't that hard, is it?
    Jop ? it's not that hard at all mateys!!! [b]


    It's no big deal to extract the first number, the split function will do that quite easily. However, none of the above have answered the question.

    My answer:
    I don't think it is possible to decalare a variable at run time using the extracted string from another variable.
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  8. #8
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    I admit I overlooked that part, in that case HeSaidJoe is right, it's impossible to declare variables at runtime
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  9. #9
    Hyperactive Member dsy5's Avatar
    Join Date
    Jul 2000
    Location
    Lockport, NY
    Posts
    362

    Re: <?>

    Originally posted by HeSaidJoe
    RobIII :It ain't that hard, is it?
    Jop ? it's not that hard at all mateys!!!



    It's no big deal to extract the first number, the split function will do that quite easily. However, none of the above have answered the question.

    My answer:
    I don't think it is possible to decalare a variable at run time using the extracted string from another variable.
    I said it first! Though, I may be wrong! (I thought I was wrong once, but I was mistaken.)
    Donald Sy - VB (ab)user

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