Results 1 to 5 of 5

Thread: extracting data from text file

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    UK
    Posts
    300

    Post

    I have a text file like this
    1020534847101000,mark,blue
    1030534877204000,karen,red
    1040534678405000,paul,green

    I want to loop through the file and only store the first
    part/number in the variable store. Then show in msgbox.
    so on the first loop store would hold 1020534847101000
    next loop would have 103... etc
    but what is happening is I get the number then I get the name
    then the color,
    How do I only get the first part, know what I mean ?


    Dim store as string

    filenum = freefile
    Open filename For Input As #filenum
    Do Until EOF(filenum)
    Input #filenum, store
    MsgBox store
    Loop

    Close #filenum

    Thanks
    > locutus

  2. #2
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892

    Post

    Try this: (In Bold are things I've added)

    Dim store as string
    Dim iPos As Integer

    filenum = freefile
    Open filename For Input As #filenum
    Do Until EOF(filenum)
    Input #filenum, store
    iPos = InStr(store, ",")
    If iPos > 0 Then store = Left(store, iPos - 1)

    MsgBox store
    Loop
    Close #filenum


    ------------------
    Yonatan
    Teenage Programmer
    E-Mail: [email protected]
    ICQ: 19552879



  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    UK
    Posts
    300

    Post

    Thanks for your reply
    but unfortunatly it didn't work. I know the easy way to do it is to declare a record or load of variables and load the textfile line that way but I was looking for a shortcut!
    Anyone ?

    >locutus

  4. #4
    Addicted Member
    Join Date
    Oct 1999
    Posts
    232

    Post

    Test it:

    -----------------
    Dim MyNumber$, MyString0$, MyString1$

    On Error Resume Next

    Open "d:\file.txt" For Input As #1
    Do While Not EOF(1)
    Input #1, MyNumber, MyString0, MyString1
    Debug.Print MyNumber
    Debug.Print MyString0
    Debug.Print MyString1
    Loop
    Close #1

    ------------------
    smalig
    [email protected]
    smalig.tripod.com

  5. #5
    Hyperactive Member Juan Carlos Rey's Avatar
    Join Date
    Aug 1999
    Location
    Mendoza, Argentina
    Posts
    301

    Post

    As there are commas in each record, each Input instruction alone will get only one variable en each line. There are two workarounds:

    1- Use Line Input instead to get the whole line and then separate the variables with InStr as Yonatan suggested:

    Line Input #1, store

    2- Use "dumb" Inputs:

    While Not Eof(1)
    Input #1 MyNumber
    MsgBox MyNumber
    Input #1 a$
    Input #1 a$
    Wend


    The last will only work if there are always one number and two strings in each line, ya know?

    Regards.

    [This message has been edited by Juan Carlos Rey (edited 11-04-1999).]

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