sprintf

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Magix

    #1

    sprintf

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

    example:
    Sandy 4001 New York City
    Joe 500001 California
    Rebecca 888 Michigan


  • Barry Schwarz

    #2
    Re: sprintf

    On Thu, 17 Mar 2005 13:53:50 +0800, "Magix" <magix@asia.com > wrote:
    [color=blue]
    >Hi,
    >How can I sprintf str1 str2 and str3 at certain position.
    > sprintf(szBuffe r, "%s %s %s", str1, str2, str3);
    >
    >example:
    >Sandy 4001 New York City
    >Joe 500001 California
    >Rebecca 888 Michigan
    >[/color]
    In your reference, review the section about length specifications that
    can be used with the %s format specification.


    <<Remove the del for email>>

    Comment

    • Magix

      #3
      Re: sprintf

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

      "Barry Schwarz" <schwarzb@deloz .net> wrote in message
      news:de9i319hfp 2u3mk0bkb55lmie up6gr6kjc@4ax.c om...[color=blue]
      > On Thu, 17 Mar 2005 13:53:50 +0800, "Magix" <magix@asia.com > wrote:
      >[color=green]
      >>Hi,
      >>How can I sprintf str1 str2 and str3 at certain position.
      >> sprintf(szBuffe r, "%s %s %s", str1, str2, str3);
      >>
      >>example:
      >>Sandy 4001 New York City
      >>Joe 500001 California
      >>Rebecca 888 Michigan
      >>[/color]
      > In your reference, review the section about length specifications that
      > can be used with the %s format specification.
      >
      >
      > <<Remove the del for email>>[/color]


      Comment

      • CBFalconer

        #4
        Re: sprintf

        *** top-posting fixed ***
        Magix wrote:[color=blue]
        > "Barry Schwarz" <schwarzb@deloz .net> wrote in message[color=green]
        >> "Magix" <magix@asia.com > wrote:
        >>[color=darkred]
        >>> How can I sprintf str1 str2 and str3 at certain position.
        >>> sprintf(szBuffe r, "%s %s %s", str1, str2, str3);
        >>>
        >>> example:
        >>> Sandy 4001 New York City
        >>> Joe 500001 California
        >>> Rebecca 888 Michigan
        >>>[/color]
        >> In your reference, review the section about length specifications
        >> that can be used with the %s format specification.[/color]
        >
        > I couldn't find any info. If u have any URL that provide relevant
        > info, please let me know.[/color]

        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.c om, 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


        Comment

        • Thomas Matthews

          #5
          Re: sprintf

          Magix wrote:
          [color=blue]
          > Hi,
          > How can I sprintf str1 str2 and str3 at certain position.
          > sprintf(szBuffe r, "%s %s %s", str1, str2, str3);
          >
          > example:
          > Sandy 4001 New York City
          > Joe 500001 California
          > Rebecca 888 Michigan
          >
          >[/color]

          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:

          C++ Faq: http://www.parashift.com/c++-faq-lite
          C Faq: http://www.eskimo.com/~scs/c-faq/top.html
          alt.comp.lang.l earn.c-c++ faq:

          Other sites:
          http://www.josuttis.com -- C++ STL Library book
          http://www.sgi.com/tech/stl -- Standard Template Library

          Comment

          • Magix

            #6
            Re: sprintf


            "Thomas Matthews" <Thomas_Matthew sSpamBotsSuck@s bcglobal.net> wrote in
            message news:KVf_d.1077 4$DW.9216@newss vr17.news.prodi gy.com...[color=blue]
            > Magix wrote:
            >[color=green]
            >> Hi,
            >> How can I sprintf str1 str2 and str3 at certain position.
            >> sprintf(szBuffe r, "%s %s %s", str1, str2, str3);
            >>
            >> example:
            >> Sandy 4001 New York City
            >> Joe 500001 California
            >> Rebecca 888 Michigan
            >>
            >>[/color]
            >
            > 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
            >[/color]

            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(szBuffe r, "%-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


            Comment

            • Tor Rustad

              #7
              Re: sprintf

              "Magix" <magix@asia.com > wrote in message
              news:423a35fd$1 _2@news.tm.net. my...

              <snip>

              [color=blue]
              > I know "-" means left-aligned, "+" means right-aligned.[/color]

              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.
              [color=blue]
              > Does %-10s mean fixed 10 character spac, allocated for that string ?[/color]

              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."

              [color=blue]
              > I tried in my case sprintf(szBuffe r, "%-30s %s", str1, str2), but[/color]
              noticed[color=blue]
              > that if str1 is more than 12 char long, the str2 will not be aligned.
              > example:
              > TestingOut1 Hello
              > TestingOut123 World
              >[/color]

              Well, let see

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

              int main( void )
              {
              printf("%-30s %s\n", "TestingOut 1", "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>


              Comment

              Working...