|
-
Feb 3rd, 2006, 02:53 AM
#1
Thread Starter
Lively Member
[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?
-
Feb 3rd, 2006, 03:05 AM
#2
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.
-
Feb 3rd, 2006, 03:13 AM
#3
Thread Starter
Lively Member
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
-
Feb 3rd, 2006, 03:23 AM
#4
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.
-
Feb 3rd, 2006, 03:31 AM
#5
Thread Starter
Lively Member
Re: Text File Problem
Can show sample code to do such? Your way is seem complicated
-
Feb 3rd, 2006, 03:36 AM
#6
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?
-
Feb 3rd, 2006, 04:00 AM
#7
Thread Starter
Lively Member
Re: Text File Problem
I cant do that, I dont understand
-
Feb 3rd, 2006, 04:20 AM
#8
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
-
Feb 3rd, 2006, 01:29 PM
#9
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...
-
Feb 3rd, 2006, 08:08 PM
#10
Re: Text File Problem
 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
-
Feb 3rd, 2006, 09:19 PM
#11
Re: Text File Problem
 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?
-
Feb 3rd, 2006, 09:23 PM
#12
Re: Text File Problem
 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
-
Feb 5th, 2006, 12:46 AM
#13
Thread Starter
Lively Member
Re: Text File Problem
Thank you. I will give your code a try.
-
Feb 5th, 2006, 12:50 AM
#14
Re: Text File Problem
 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?
-
Feb 10th, 2006, 11:27 PM
#15
Thread Starter
Lively Member
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.
-
Feb 10th, 2006, 11:30 PM
#16
Re: Text File Problem
You may just be missing some imports statements or something... are you getting blue squigglies? What is the problem?
-
Feb 10th, 2006, 11:37 PM
#17
Thread Starter
Lively Member
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?
-
Feb 10th, 2006, 11:41 PM
#18
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.
-
Feb 13th, 2006, 09:41 PM
#19
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|