CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2004
    Posts
    7

    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.

  2. #2
    Join Date
    Oct 2004
    Location
    Australia, Yeppoon
    Posts
    44

    Question 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".

  3. #3
    Join Date
    Jun 2002
    Location
    West Virginia
    Posts
    131

    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

  4. #4
    Join Date
    Oct 2004
    Posts
    7

    Re: Reading first word per line in *.txt file.

    Yes it does have a space, Thanks. I'll try that out.

  5. #5
    Join Date
    Oct 2004
    Posts
    7

    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.

  6. #6
    Join Date
    Jul 2004
    Location
    Jakarta, Indonesia
    Posts
    596

    Re: Reading first word per line in *.txt file.

    Code:
    LastName = Left$(temp, InStr(temp, " ") - 1)
    don't use '$'..

    1st NF - a table should not contain repeating groups.
    2nd NF - any fields that do not depend fully on the primary key should be moved to another table.
    3rd NF - there should be no dependency between non key fields in same table.
    - E. Petroutsos -


    eRiCk

    A collection of "Laku-abis" Ebook, Permanent Residence

    Access Reserved Words, a Classic Form Bug, Access Limitation, Know run Process and the Lock they hold in, Logging User Activity in MSSQL

  7. #7
    Join Date
    Jun 2002
    Location
    West Virginia
    Posts
    131

    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
  •  





Click Here to Expand Forum to Full Width

Featured