sprintf

Discussion in 'C Programming' started by Magix, Mar 17, 2005.

  1. Magix

    Magix Guest

    Hi,
    How can I sprintf str1 str2 and str3 at certain position.
    sprintf(szBuffer, "%s %s %s", str1, str2, str3);

    example:
    Sandy 4001 New York City
    Joe 500001 California
    Rebecca 888 Michigan
     
    Magix, Mar 17, 2005
    #1
    1. Advertising

  2. On Thu, 17 Mar 2005 13:53:50 +0800, "Magix" <> wrote:

    >Hi,
    >How can I sprintf str1 str2 and str3 at certain position.
    > sprintf(szBuffer, "%s %s %s", str1, str2, str3);
    >
    >example:
    >Sandy 4001 New York City
    >Joe 500001 California
    >Rebecca 888 Michigan
    >

    In your reference, review the section about length specifications that
    can be used with the %s format specification.


    <<Remove the del for email>>
     
    Barry Schwarz, Mar 17, 2005
    #2
    1. Advertising

  3. Magix

    Magix Guest

    I couldn't find any info. If u have any URL that provide relevant info,
    please let me know.

    "Barry Schwarz" <> wrote in message
    news:...
    > On Thu, 17 Mar 2005 13:53:50 +0800, "Magix" <> wrote:
    >
    >>Hi,
    >>How can I sprintf str1 str2 and str3 at certain position.
    >> sprintf(szBuffer, "%s %s %s", str1, str2, str3);
    >>
    >>example:
    >>Sandy 4001 New York City
    >>Joe 500001 California
    >>Rebecca 888 Michigan
    >>

    > In your reference, review the section about length specifications that
    > can be used with the %s format specification.
    >
    >
    > <<Remove the del for email>>
     
    Magix, Mar 17, 2005
    #3
  4. Magix

    CBFalconer Guest

    *** top-posting fixed ***
    Magix wrote:
    > "Barry Schwarz" <> wrote in message
    >> "Magix" <> wrote:
    >>
    >>> How can I sprintf str1 str2 and str3 at certain position.
    >>> sprintf(szBuffer, "%s %s %s", str1, str2, str3);
    >>>
    >>> example:
    >>> Sandy 4001 New York City
    >>> Joe 500001 California
    >>> Rebecca 888 Michigan
    >>>

    >> In your reference, review the section about length specifications
    >> that can be used with the %s format specification.

    >
    > I couldn't find any info. If u have any URL that provide relevant
    > info, please let me know.


    Look in your C book. Also google for N869 or try the dinkum pages.

    Meanwhile, don't toppost. Topposters make us mean and angry, and
    we become most unpleasant.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
     
    CBFalconer, Mar 17, 2005
    #4
  5. Magix wrote:

    > Hi,
    > How can I sprintf str1 str2 and str3 at certain position.
    > sprintf(szBuffer, "%s %s %s", str1, str2, str3);
    >
    > example:
    > Sandy 4001 New York City
    > Joe 500001 California
    > Rebecca 888 Michigan
    >
    >


    Try %#s where # is the field width:
    printf("%-10s%6d %-20s",
    "Sandy", 4001, "New York City\n");

    Look up in your C reference book the term
    "format specifiers". If you don't have
    a book get one. If your book doesn't have
    this term in the index, get a different book.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library
     
    Thomas Matthews, Mar 17, 2005
    #5
  6. Magix

    Magix Guest

    "Thomas Matthews" <> wrote in
    message news:KVf_d.10774$...
    > Magix wrote:
    >
    >> Hi,
    >> How can I sprintf str1 str2 and str3 at certain position.
    >> sprintf(szBuffer, "%s %s %s", str1, str2, str3);
    >>
    >> example:
    >> Sandy 4001 New York City
    >> Joe 500001 California
    >> Rebecca 888 Michigan
    >>
    >>

    >
    > Try %#s where # is the field width:
    > printf("%-10s%6d %-20s",
    > "Sandy", 4001, "New York City\n");
    >
    > Look up in your C reference book the term
    > "format specifiers". If you don't have
    > a book get one. If your book doesn't have
    > this term in the index, get a different book.
    >
    > --
    > Thomas Matthews
    >


    I know "-" means left-aligned, "+" means right-aligned.
    Does %-10s mean fixed 10 character spac, allocated for that string ?
    I tried in my case sprintf(szBuffer, "%-30s %s", str1, str2), but noticed
    that if str1 is more than 12 char long, the str2 will not be aligned.
    example:
    TestingOut1 Hello
    TestingOut123 World
     
    Magix, Mar 18, 2005
    #6
  7. Magix

    Tor Rustad Guest

    "Magix" <> wrote in message
    news:423a35fd$...

    <snip>


    > I know "-" means left-aligned, "+" means right-aligned.


    No, "+" flag is not relevant for the %s conversion. "+" flag
    make printf show the sign (+ or -). "-" flag means left justified,
    while the default is right justified.

    > Does %-10s mean fixed 10 character spac, allocated for that string ?


    No. 10 means minimum field width.

    "Characters from the array are written up to (but not including) the
    terminating null character. If the precision is specified, no more than
    that many characters are written."


    > I tried in my case sprintf(szBuffer, "%-30s %s", str1, str2), but

    noticed
    > that if str1 is more than 12 char long, the str2 will not be aligned.
    > example:
    > TestingOut1 Hello
    > TestingOut123 World
    >


    Well, let see

    C:\Temp>type test.c
    #include <stdio.h>

    int main( void )
    {
    printf("%-30s %s\n", "TestingOut1", "Hello");
    printf("%-30s %s\n", "TestingOut123", "World");

    return 0;
    }

    C:\Temp>cl /Za test.c
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for
    80x86
    Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

    test.c
    Microsoft (R) Incremental Linker Version 6.00.8447
    Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

    /out:test.exe
    test.obj

    C:\Temp>test
    TestingOut1 Hello
    TestingOut123 World

    which was displayed _exactly_ as expected. Perhaps try to watch
    it in Courier font...

    --
    Tor <torust AT online DOT no>
     
    Tor Rustad, Mar 18, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shea martin

    sprintf

    shea martin, Sep 2, 2004, in forum: Java
    Replies:
    5
    Views:
    3,608
    shea martin
    Sep 3, 2004
  2. Pep
    Replies:
    5
    Views:
    4,084
  3. CJ
    Replies:
    1
    Views:
    1,355
    Davlet Panech
    Oct 28, 2003
  4. Mike Chirico
    Replies:
    2
    Views:
    3,867
    Grumble
    Nov 19, 2003
  5. Pilatus
    Replies:
    3
    Views:
    553
    Pilatus
    Dec 18, 2003
Loading...

Share This Page