|
-
October 30th, 2004, 03:55 AM
#1
Reading first word per line in *.txt file.
Hello,
I have a text file it contains name, addresses, phone number, etc. like such
Welterson Steve 213 N. Sureburn 555-5555 Misc Notes\Info
Sanderson Joel 782 S. Unsureburn 555-5333 Misc Notes\Info
etc.
I just want to get the first word, the surname, and put it in it's own text file.
Can it be done? Thanks alot.
-
October 30th, 2004, 05:13 AM
#2
Re: Reading first word per line in *.txt file.
When ever I use text files for database data I usually write the information to the file using a carriage return between data type. e.g
tempstring = name + chr(13) + address + chr(13) + phone etc
to show:
Name
address
phone
Name (here it begins the next record)
address
phone
etc.
this way to read from the file it is very simple so that you read one line at a time. why not change your file to taht and then just simply use "line input".
-
October 30th, 2004, 05:56 AM
#3
Re: Reading first word per line in *.txt file.
If a space follows the last name then use.
LastName=Left$(text,InStr$(text," ")-1)
Wayne
-
October 31st, 2004, 11:04 PM
#4
Re: Reading first word per line in *.txt file.
Yes it does have a space, Thanks. I'll try that out.
-
November 1st, 2004, 12:14 AM
#5
Re: Reading first word per line in *.txt file.
okay, I know i'm doing something really stupid, because It's not working
Code:
Private Sub Command1_Click()
Dim LastName As String
Open "C:\Information.txt" For Input As #1
Open "C:\lastnames.txt" For Append As #2
While Not EOF(1)
Line Input #1, temp$
LastName = Left$(temp$, InStr(temp$, " ") - 1)
Print #2, LastName
Wend
Close #1
End Sub
It says LastName is an invalid procedure call or argument. But I can see that the LastName string has a name in it!! and the file I'm Printing to has data? But I still get error?
UPDATE: okay that does work but it found some lines that only had a lastname with no other info so it errored. I suppose I can just remove those lines or add an On Error Resume Next?
Last edited by cereal_1; November 1st, 2004 at 12:36 AM.
-
November 1st, 2004, 12:30 AM
#6
Re: Reading first word per line in *.txt file.
Code:
LastName = Left$(temp, InStr(temp, " ") - 1)
don't use '$'..
-
November 1st, 2004, 05:55 AM
#7
Re: Reading first word per line in *.txt file.
Don't use "On Error Resume Next". It could cause more problems.
Break up the line like this:
Code:
Dim x as Integer, Text as String
x=InStr(Text, " ")
If x=0 Then
Print #2, Text
Else
Print #2, Left$(Text,x-1)
End If
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|