Results 1 to 2 of 2

Thread: Array write to file junk data

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2008
    Posts
    1

    Angry Array write to file junk data

    All,

    I am reading user input from a PowerPoint text box. I store this information to a file. I then read the file back in as a string, and split it using the period as the delimiter. When I use a message box to show the contents of the newly created array, it is fine. Then when I write the contents of the array back to a file, I get garbage data inserted. Here are what the two files look like:

    Stored from the text box on the PowerPoint slide is the following:

    Text one. Text two. Text three.

    Stored from writing the array to a file shows up as Chinese characters when inserted into my post, so I'll attach both files instead. I'm using the following code to open the files and write to them:

    ##############################################
    Dim fname As String
    Dim rec As String 'receive string to put the text file into
    Dim i As Integer
    fname = "C:\demo.txt" 'file to write text box info to (I perform a check if the file isn't there and write a file with blank content - not shown)
    i = FreeFile
    Open fname For Input As #i
    Do While Not EOF(i)
    Input #i, rec
    Loop
    Close #i
    ################################################
    I then split the string to create the array of elements
    ################################################
    Dim elements() As String
    Dim lngPosition As Long
    elements = split (rec,".")
    For lngPosition = LBound(elements) To UBound(elements)

    MsgBox elements(lngPosition)

    Next lngPosition
    ################################################
    Write array back to a text file
    ################################################
    Dim j As Integer
    j = FreeFile
    fname = "C:\arrayout.txt"
    Open fname For Output As #j
    Put #j, ,elements 'can I use the vbCr here?
    Close #j
    ###############################################

    Thanks!
    Attached Files Attached Files

  2. #2
    Addicted Member ThorSubak's Avatar
    Join Date
    Apr 2008
    Location
    Cebu
    Posts
    153

    Re: Array write to file junk data

    in this part..
    vb Code:
    1. Dim j As Integer
    2. j = FreeFile
    3. fname = "C:\arrayout.txt"
    4. Open fname For Output As #j
    5. Put #j, ,elements 'you should not put directly the data automatically since the variable is an array and it assumes to be not equivalent to your original data.
    6. Close #j

    try this..

    vb Code:
    1. Dim j As Integer
    2. j = FreeFile
    3. fname = "C:\arrayout.txt"
    4. Open fname For Output As #j
    5.   For x=0 To uBound(elements)
    6.     Put #j, ,elements(x)
    7.   Next
    8. Close #j

    : the code is not tested:
    : gues i help:
    __________________
    if the problem is solved, please mark your post as RESOLVED and please do rate.

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