[RESOLVED] Need Urgent Help From You All
Dear Experts Out There,
I need your help urgently.
I would like to write VB code which could do the followings:
1. Read lines of strings from input text file (the file has few lines, each line has few words)
2. Then the code will split the strings from each line if it found " " (space)
3. The code will write the strings to output file; one word in each line such as:
Original file input content
I love you so much
You are my sunshine
What will happen tomorrow
The content of output file are:
I
will
love
you
so
much
......
......
Thanks guys ;) ;)
Re: Need Urgent Help From You All
Welcome to the forums!
First thing, don't post your email address in a public forum, or you will get spammed after the bots pick it up. Edit that post, and remove it.
Two, we don't write apps for people, even if they aren't hard to do. If we do the work for you, would you learn anything?
Third, there is the FAQ forum, where you will find a link called File Basics, which show you how to do file operations in Visual Basic.
Fourth, after you try to get something working, and run across a problem, post the code, the error, and the error line. Someone will try to help you resolve it.
Re: Need Urgent Help From You All
lemme do this thing too...
welcome to the forums.
first thing, well said @ dglienna
second thing, nothing
third thing, that's all
hope you learned your thing ;)
Re: Need Urgent Help From You All
Great answer and advice from you guys.
Well as I said, my codes are just like these:
'reading from text file which contain strings
Do Until EOF(1)
Contents = Input$(LOF(strFileInput), #1)
' Split the contents into lines.
lines = Split(Contents, " ")
Write #2, lines
Loop
The above codes just copy the whole lines and write them to outfile but dont do what I wanted.
Please help again. Thanks
Re: Need Urgent Help From You All
You are trying to write the whole array at once, whereas you would have to loop through the array that you get when reading from the File. Your modified code should look like this
VB Code:
Dim sTemp As String, arrayBound As Long, loopCounter As Long
Do Until EOF(1)
Contents = Input$(LOF(strFileInput), #1)
' Split the contents into lines.
lines = Split(Contents, " ")
arrayBound = UBound(lines) - 1
For loopCounter = 0 To arrayBound
Write #2, lines(loopCounter)
Next
Loop
Re: Need Urgent Help From You All
Dear Shuja ALi,
I guess you don't get what I am trying to do. If you look at the original posting, that is what I wanted.
Just any codes or whatever mean as long I could get each word in the lines written to output file; one word for each line.
Thanks Shuja Ali. I have tested your codes and they dont work. Could you please modified my code again. But, is there any other way to do it without array?
Re: Need Urgent Help From You All
I know you are new to the forums but you don't have to say "Dear so and so" for nearly every post. Treat posts like you are having a conversation, not like penpal mail you send to friends. ;)
Re: Need Urgent Help From You All
i'm not sure if this code of mine would work with multiple lines of text but just try this function
Function readFile() As String
On Error GoTo trap
Dim a As Variant
a = FreeFile
Open App.Path & "\<filename.extension>" For Input As #a
readFile = Input(LOF(a), #a)
Exit Function
Close a
trap:
If Err.Number = 53 Then
'the file is not found
End
End If
End Function
Re: Need Urgent Help From You All
to use that function, do it like this
text1.text = readfile
Re: Need Urgent Help From You All
Quote:
Originally Posted by guy_toforget
Dear Shuja ALi,
I guess you don't get what I am trying to do. If you look at the original posting, that is what I wanted.
Just any codes or whatever mean as long I could get each word in the lines written to output file; one word for each line.
Thanks Shuja Ali. I have tested your codes and they dont work. Could you please modified my code again. But, is there any other way to do it without array?
Ok so you don't want arrays in your code. Here is an idea of how you can do it. Read all the contents from the file at once, replace the spaces with the Carriage Return and Line feed(vbcrlf) and then write the contents back to the file. This is how I did it
VB Code:
Dim sContent As String
Open "C:\somefile.txt" For Input As #1
Open "C:\anotherFile.txt" For Output As #2
sContent = Input$(LOF(1), #1)
sContent = Replace(sContent, " ", vbCrLf)
Print #2, sContent
Close #1
Close #2
Now remember as the File grows in size, this function will become more and more slower. And there will be an upper limit as to how much you can keep in a string variable.
Re: Need Urgent Help From You All
Great job Shuja Ali. Your codes really work
Re: Need Urgent Help From You All
yea, really nice code with just few statements and it worked.
well done shuja ali
Re: Need Urgent Help From You All
I guess Shuja and Ganesh could help again with this:
Well, now I need to do another task without using array too.
I want to
1. sort the strings in the text file
2. count the frequency of occurance for each string
3. save to the output file such as:
word frequency
lunch 5
dinner 10
Thanks.
Re: Need Urgent Help From You All
how many times do we have to "advise" you that we dont code for you guys, we only help with your existing code...http://www.smileypad.com/v10/Cache/Angry/Furious.gifhttp://www.smileypad.com/v10/Cache/Angry/Weak-Link.gif
Re: Need Urgent Help From You All
I think we are not feeding any baby here, you try to do it on your own and not just rely always on others. the code thatShuja ali had posted can be modified to solve your problem. i think you can do it with your own with that code.
Re: Need Urgent Help From You All
Why don't you try it yourself first. Maybe you will learn something during excercise. It is always better to try to explore as many options as you have before posting it in the Forums.
Re: Need Urgent Help From You All
Ok, this is my code for the question
VB Code:
Private Sub Command1_Click()
Dim strWord, strFirstWord, strSecondWord As String
Dim intCount As Integer
Dim i, j As Integer
'strWord is the string to read in text file
'strFirstWord is the first string
'strSecondWord is the second; then compare the first with the second
Open "c:\testing.txt" For Input As #1
Open "c:\result.txt" For Output As #2
While Not EOF(1)
intCount = 0
Line Input #1, strWord
strFirstWord = strWord
intCount = 1
Line Input #1, strWord
strSecondWord = strWord
If strFirstWord = strSecondWord Then
Do
intCount = intCount + 1
strFirstWord = strSecondWord
Line Input #1, strWord
strSecondWord = strWord
Loop While (strWord = strSecondWord)
Write #2, strWord, intCount
Else
intCount = 0
strFirstWord = strWord
Do
intCount = intCount + 1
Line Input #1, strWord
strSecondWord = strWord
Loop While (strWord = strFirstWord)
Write #2, strWord, intCount
End If
Wend
End Sub
Thanks
Re: Need Urgent Help From You All
then is there any problem with it?
Re: Need Urgent Help From You All
well, I have got the answer for my problem; but the output is so strange.
I read lines of strings from file, convert them into arrays of strings; sort them using bubble sort & quicksort; but the strings are not really sorted; what is wrong.
Here are the codes
[vb]
dim strAll() as String
sContent = Input$(LOF(1), #1)
strAll = Split(sContent, " ")
'Bubble sort it.
lLwrBnd = LBound(strAll())
lUpBnd = UBound(strAll())
For h = lLwrBnd To lUpBnd
For j = h To lUpBnd
If strAll(h) > strAll(j) Then
sTemp = strAll(h)
strAll(h) = strAll(j)
strAll(j) = sTemp
End If
Next j
Next h
'Print the results.
For h = lLwrBnd To lUpBnd
Debug.Print strAll(h)
Next h
Re: Need Urgent Help From You All
Thanks guys. My problems are solved