PDA

Click to See Complete Forum and Search --> : extracting data from text file


locutus
Nov 2nd, 1999, 10:35 PM
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

Yonatan
Nov 2nd, 1999, 10:41 PM
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: RZvika@netvision.net.il
ICQ: 19552879 (http://www.icq.com/19552879)

locutus
Nov 3rd, 1999, 05:24 PM
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

smalig
Nov 3rd, 1999, 05:36 PM
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
smalig@hotmail.com
smalig.tripod.com (http://smalig.tripod.com)

Juan Carlos Rey
Nov 4th, 1999, 01:34 AM
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).]