Results 1 to 19 of 19

Thread: [RESOLVED] Text File Problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    80

    Resolved [RESOLVED] Text File Problem

    I have text file content like this:

    have
    a
    dream
    A
    dream
    of
    reality
    and
    fantasy

    How to convert each word into array of strings?

    My code look like this but doesnt work

    Dim myarray() as String
    Dim strWord as String
    Dim i,x,k, Upper,Lower as Integer
    i = LOF(1)
    x=0
    while not EOF(1)
    input #1, strWord
    myarray(x) = strWord
    x=x+1
    wend
    Lower=LBound(myarray)
    Upper=UBound(myarray)

    For k=LBound(myarray()) to UBound(myarray())
    Print myarray(k)

    Why it doesnt convert each word as array of string?
    How to use UBound(myarray) to print all strings?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Text File Problem

    First off, forget your wicked VB6 ways and read up on the StreamReader class. If you have a single word on each line then the thing to do is open a StreamReader and call ReadToEnd, which gets the whole file as a single string. You can then split that string on the line breaks using System.Text.RegularExpressions.Regex.Split.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    80

    Re: Text File Problem

    well, if each line has few strings, how to make them then? Such as

    Hello please help this kids
    He need you so much
    Hello 49 89 66
    Can or not

    Please help as I am really a newbie to this VB

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Text File Problem

    If that's the case then you can split each line on the space character. My advice would be to use a StreamReader still but call ReadLine to get one line at a time. You can call String.Split on that line to get an String array containing the individual words. You can add this array to a Specialized.StringCollection using AddRange. Once you've read every line you can call CopyTo on the StringCollection to copy all the strings to an array if required.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    80

    Re: Text File Problem

    Can show sample code to do such? Your way is seem complicated

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Text File Problem

    It's not complicated but it's a good idea to do it one step at a time. Can you open the StreamReader and read each line?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    80

    Re: Text File Problem

    I cant do that, I dont understand

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Text File Problem

    And have you looked up the StreamReader class in the help/MSDN and read about it to see how it gets used? I tried to give you a push but it appears that you just want it done for you:
    Code:
            Dim sr As New IO.StreamReader("file path here")
            Dim sc As New Specialized.StringCollection
    
            Do Until sr.Peek() = -1
                'Read the line, split it at all spaces and add the individual words to the collection
                sc.AddRange(sr.ReadLine().Split(" "c))
            Loop
    
            sr.Close()
    
            'Loop through the collection and display each one individually.
            For Each word As String In sc
                MessageBox.Show(word)
            Next word
    
            'Create an array to store the words.
            Dim words(sc.Count - 1) As String
    
            'Copy the words from the collection to an array if required.
            sc.CopyTo(words, 0)
    
            'Loop through the array and display each one individually.
            For Each word As String In words
                MessageBox.Show(word)
            Next word
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Text File Problem

    There are also ample examples in this forum if you just search for "split text" or "read text", or something to that matter...

  10. #10
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Text File Problem

    Quote Originally Posted by jmcilhinney
    And have you looked up the StreamReader class in the help/MSDN and read about it to see how it gets used? I tried to give you a push but it appears that you just want it done for you:
    Code:
    ...Insert Code Here...
    I think you may have done his homework for him. heh
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Text File Problem

    Quote Originally Posted by kasracer
    I think you may have done his homework for him. heh
    I know. That's the main reason I was being cagey to begin with, but some people just won't break a sweat themselves. I should have just walked away but a few expletives and some quick typing later... you've seen the result. Should take my own advice occasionally shouldn't I?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Text File Problem

    Quote Originally Posted by jmcilhinney
    I know. That's the main reason I was being cagey to begin with, but some people just won't break a sweat themselves. I should have just walked away but a few expletives and some quick typing later... you've seen the result. Should take my own advice occasionally shouldn't I?

    That's why you only have 400+ reps instead of 500+

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    80

    Re: Text File Problem

    Thank you. I will give your code a try.

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Text File Problem

    Quote Originally Posted by conipto
    That's why you only have 400+ reps instead of 500+

    Bill
    That's just cruel. Did it ever occur to you that I may be sensitive and my confidence easily shaken?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    80

    Re: Text File Problem

    Thanks for your code. Though it doesn't work in my project, you are very professional and good tutor. I dont know which components of Vb should I add to make it work.

  16. #16
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Text File Problem

    You may just be missing some imports statements or something... are you getting blue squigglies? What is the problem?

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    80

    Re: Text File Problem

    Perhaps you are having that sympthon. I am only asking how to count the occurances of every strings in a text file. Could you help?

  18. #18
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Text File Problem

    No, in your first post, you were simply wanting to convert the file to an array of strings, which the code already given to you does.

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    80

    Re: Text File Problem

    MY file problem is solved. Thanks guys

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