simple printf question

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

    simple printf question

    Hi,

    I have a giant string buffer, and I want to print out small chunks of it at
    a time. How do I print out, say 20 characters of a string?

    Is it like this?

    printf("%20s",m ystring);

    I can change the start point of the string, I just don't know how to tell it
    to only print out X number of characters from it.
    Thanks
    B


  • Richard Heathfield

    #2
    Re: simple printf question

    Bint said:
    Hi,
    >
    I have a giant string buffer, and I want to print out small chunks of it
    at
    a time. How do I print out, say 20 characters of a string?
    >
    Is it like this?
    >
    printf("%20s",m ystring);
    >
    I can change the start point of the string, I just don't know how to tell
    it to only print out X number of characters from it.
    printf("%.20s", mystring);

    Note the dot in the format specifier.

    Covered in K&R2 p244.

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • Tor Rustad

      #3
      Re: simple printf question

      Bint wrote:
      Hi,
      >
      I have a giant string buffer, and I want to print out small chunks of it at
      a time. How do I print out, say 20 characters of a string?
      >
      Is it like this?
      >
      printf("%20s",m ystring);
      printf("%.*s", len, mystring);


      --
      Tor <torust [at] online [dot] no>

      C-FAQ: http://c-faq.com/

      Comment

      • husterk

        #4
        Re: simple printf question

        On Oct 17, 1:37 pm, "Bint" <b...@csgs.comw rote:
        Hi,
        >
        I have a giant string buffer, and I want to print out small chunks of it at
        a time. How do I print out, say 20 characters of a string?
        >
        Is it like this?
        >
        printf("%20s",m ystring);
        >
        I can change the start point of the string, I just don't know how to tell it
        to only print out X number of characters from it.
        Thanks
        B
        Bint,

        Another more flexible method would be to snprintf() your larger string
        into a temporary string buffer and then output the temporary buffer
        using a standard unformatted printf(). This will allow you to
        dynamically change the size of your output string by supplying a
        variable length for your temporary string buffer in the snprintf()
        routine (which cannot be accomplished using the "%.20s" method
        described by Richard.

        Keith


        Comment

        Working...