Results 1 to 6 of 6

Thread: format text when saving to a file

  1. #1
    Guest

    Post

    let's say I got a 5 MB long string in memory.
    I want to save it as a file with no more that 65 bytes per line.
    looping sounds horrible...it takes so much time

    is there a function doing this ?


  2. #2
    Hyperactive Member badgers's Avatar
    Join Date
    Sep 1999
    Location
    Madison, WI USA
    Posts
    444

    Post

    when you say 65 bytes per line, do you mean with word wrapping?

  3. #3
    Guest

    Post

    yes that's what I mean
    I can loop with the mid function but it's painfully long; I wonder if there is a function writing to a file and wrapping in function of a parameter

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,175

    Post

    Well, I don't know if my solution is shorter than your "painfully long" one, but check my answer to another post.

    ------------------
    Marty
    Why is it called lipstick if you can still move your lips?

  5. #5
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,176

    Post

    Here's a possible Solution I came up with, it uses the CopyMemory API to Transfer the String into an Array of User Types Each Containing an Array of 65 Bytes, which automatically and Very quickly segments the string into 65 Byte Portions which you can then output to a File:
    Code:
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    
    Private Type MemStringType
        sString(64) As Byte
    End Type
    
    Private Sub OutputFile(ByVal FileName As String, ByVal Data As String)
        Dim tStrings() As MemStringType
        Dim iFile As Integer
        Dim iIndex As Long
        
        ReDim tStrings(Len(Data) / 65)
        CopyMemory tStrings(0), ByVal Data, LenB(tStrings(0)) * UBound(tStrings)
        iFile = FreeFile
        Open FileName For Output As iFile
            For iIndex = 0 To UBound(tStrings)
                Print #iFile, StrConv(tStrings(iIndex).sString, vbUnicode)
            Next
        Close iFile
    End Sub
    
    Private Sub Command1_Click()
        OutputFile "C:\TheFile.txt", sTheBig5MBString
    End Sub
    I tested this passing it a String of 5 Million "A"'s equivalent to 5MB of Data and it took only a matter of seconds.

    ------------------
    Aaron Young
    Analyst Programmer
    aarony@redwingsoftware.com
    ajyoung@pressenter.com


  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,175

    Post

    When yaz said "no more that 65 bytes per line", I assumed he meant that he wanted to break the lines at the end of words.

    ------------------
    Marty
    Why is it called lipstick if you can still move your lips?

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