Results 1 to 6 of 6

Thread: Write padded string to a file - how?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    London
    Posts
    290
    Easy question, i`m sure, but its not obvious from the `Format` command help.

    I have a simple string, which contains, 10 chars. But i want to write it to a file, padded out (with spaces) to 20 chars.

    I`m after something like:

    Line = Line & Format(!FieldFromDb, 20)

    Any ideas?

    Thanks,
    Alex.


  2. #2
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    there's a Space$ function in vb that will fill a string with spaces Space$(5) returns a string of 5 spaces
    so you could try something like

    line=line & Space$(5) & fieldfromDb & space$(5)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    London
    Posts
    290
    weeeeellll... the len of the string is variable, so i`d need to subtract the len of my string from the total, and use that as the parameter to space. I was rather hoping there`d be a function that`d do it for me...

    perhaps there isnt though!

    a.

  4. #4
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Pallex, there is...

    This is a little trick & learned from my Access programming days that works in VB too:
    Code:
    Line = Line & Left(!FieldFromDB & "                    ", 20)
    Regardless of the length of your field, it will be right-padded so that the total string has a length of 20. Remember, if you use some number other than 20 you have to change the number of spaces to match that number or you'll get weird results. For example:
    Code:
    Line = Line & Left(!FieldFromDB & "     ", 5)
    All the best.

  5. #5
    Lively Member
    Join Date
    Jan 1999
    Location
    México, D.F.
    Posts
    84
    Hi.
    If you want to add spaces after the string but the spaces and the string must be 20, do this:

    first declare a variable type string like this;

    Dim sString as String *20

    This reserve 20 spaces in memory.

    Then write it into the file, I think that you're using the Open file function, isn't? Ok, then open a file to write with this:

    Open (path-file) for Output as #(filenumber)
    Print #(filenumber), sString
    Close #(filenumber)

    Where (path-file) is the path and name of your file, (filenumber) is the number type integer that you can get it with Freefile function or write it.

    This write into the file the string and the spaces but always going to be 20, it's useful if you want to do a table in a text file.

    I hope that this can help you.

    Best Regards to everyone and have a nice weekend!!
    Angel Maldonado López.
    VB Programmer

  6. #6
    Hyperactive Member Juan Carlos Rey's Avatar
    Join Date
    Aug 1999
    Location
    Mendoza, Argentina
    Posts
    301

    Also you can try:

    Line = Line & Space$(20 - Len(Line))

    This adds enough spaces to reach 20 characters. Be sure, thou, that there are no more than 20 characters in your original Line!


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