Results 1 to 20 of 20

Thread: [RESOLVED] Need Urgent Help From You All

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    80

    Resolved [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
    Last edited by Hack; Feb 14th, 2006 at 07:38 AM. Reason: Added [RESOLVED] to thread title and green "resolved" checkmark

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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.

  3. #3
    Fanatic Member lerroux's Avatar
    Join Date
    Nov 2005
    Location
    Welcome to the darkside... we have cookies
    Posts
    646

    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
    WARNING: Excessive coding is dangerous to your health... if symptoms persist insult your doctor...

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    80

    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

  5. #5
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    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:
    1. Dim sTemp As String, arrayBound As Long, loopCounter As Long
    2.     Do Until EOF(1)
    3.  
    4.         Contents = Input$(LOF(strFileInput), #1)
    5.  
    6.         ' Split the contents into lines.
    7.         lines = Split(Contents, " ")
    8.         arrayBound = UBound(lines) - 1
    9.         For loopCounter = 0 To arrayBound
    10.             Write #2, lines(loopCounter)
    11.         Next
    12.     Loop

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    80

    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?

  7. #7
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    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.

  8. #8
    Frenzied Member d3gerald's Avatar
    Join Date
    Jan 2006
    Posts
    1,348

    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

  9. #9
    Frenzied Member d3gerald's Avatar
    Join Date
    Jan 2006
    Posts
    1,348

    Re: Need Urgent Help From You All

    to use that function, do it like this

    text1.text = readfile

  10. #10
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    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:
    1. Dim sContent As String
    2.     Open "C:\somefile.txt" For Input As #1
    3.     Open "C:\anotherFile.txt" For Output As #2
    4.     sContent = Input$(LOF(1), #1)
    5.     sContent = Replace(sContent, " ", vbCrLf)
    6.     Print #2, sContent
    7.     Close #1
    8.     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.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    80

    Re: Need Urgent Help From You All

    Great job Shuja Ali. Your codes really work

  12. #12
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: Need Urgent Help From You All

    yea, really nice code with just few statements and it worked.
    well done shuja ali

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    80

    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.

  14. #14
    Fanatic Member lerroux's Avatar
    Join Date
    Nov 2005
    Location
    Welcome to the darkside... we have cookies
    Posts
    646

    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...
    WARNING: Excessive coding is dangerous to your health... if symptoms persist insult your doctor...

  15. #15
    Frenzied Member d3gerald's Avatar
    Join Date
    Jan 2006
    Posts
    1,348

    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.
    On error goto Trap

    Trap:
    in case of emergency, drop the case...

    ****************************************
    If this post has been resolved. Please mark it as "Resolved" by going through the "Thread Tools" above and clicking on the "Mark Thread Resolved " option.
    if a post is helpful to you, Please Rate it by clicking on the Rate link right below the avatar

  16. #16
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    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.

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    80

    Re: Need Urgent Help From You All

    Ok, this is my code for the question
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim strWord, strFirstWord, strSecondWord  As String
    3. Dim intCount As Integer
    4. Dim i, j As Integer
    5. 'strWord is the string to read in text file
    6. 'strFirstWord is the first string
    7. 'strSecondWord is the second; then compare the first with the second
    8.  
    9.  
    10. Open "c:\testing.txt" For Input As #1
    11. Open "c:\result.txt" For Output As #2
    12.  
    13. While Not EOF(1)
    14.     intCount = 0
    15.     Line Input #1, strWord
    16.     strFirstWord = strWord
    17.     intCount = 1
    18.     Line Input #1, strWord
    19.     strSecondWord = strWord
    20.     If strFirstWord = strSecondWord Then
    21.         Do
    22.         intCount = intCount + 1
    23.         strFirstWord = strSecondWord
    24.         Line Input #1, strWord
    25.         strSecondWord = strWord
    26.         Loop While (strWord = strSecondWord)
    27.         Write #2, strWord, intCount
    28.      Else
    29.         intCount = 0
    30.         strFirstWord = strWord
    31.          Do
    32.             intCount = intCount + 1
    33.             Line Input #1, strWord
    34.             strSecondWord = strWord
    35.          Loop While (strWord = strFirstWord)
    36.          Write #2, strWord, intCount
    37.     End If
    38.              
    39. Wend
    40.    
    41. End Sub
    Thanks
    Last edited by si_the_geek; Jan 26th, 2006 at 09:26 AM. Reason: added VBCode tags

  18. #18
    Frenzied Member d3gerald's Avatar
    Join Date
    Jan 2006
    Posts
    1,348

    Re: Need Urgent Help From You All

    then is there any problem with it?
    On error goto Trap

    Trap:
    in case of emergency, drop the case...

    ****************************************
    If this post has been resolved. Please mark it as "Resolved" by going through the "Thread Tools" above and clicking on the "Mark Thread Resolved " option.
    if a post is helpful to you, Please Rate it by clicking on the Rate link right below the avatar

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    80

    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

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    80

    Re: Need Urgent Help From You All

    Thanks guys. My problems are solved

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