How to printf 'n' characters from a char* to a file?

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

    How to printf 'n' characters from a char* to a file?

    Hi,

    Can you please tell me how can I printf 'n' characters of from a char*
    to a file.

    I notice there is a snprinf, but I don't see one corresponding to
    fprintf?

    I am thinking if i can do that without an extra memcpy.

    Thank you for any help.
  • Walter Roberson

    #2
    Re: How to printf 'n' characters from a char* to a file?

    In article <8a1453f8-576f-415c-9d89-f2852bc6af6a@t5 4g2000hsg.googl egroups.com>,
    Jonathan <Jonathan.Crane 08@gmail.comwro te:
    >Can you please tell me how can I printf 'n' characters of from a char*
    >to a file.
    >I notice there is a snprinf, but I don't see one corresponding to
    >fprintf?
    >I am thinking if i can do that without an extra memcpy.
    printf("%.*s", n, TheCharPointer) ;

    Or if n is known ahead of time and fixed, it can be hard-coded,
    such as

    printf("%.5s", TheCharPointer) ;

    If the string does not contain at least n characters, you have
    a problem. Also, the behaviour is not explicitly defined
    (at least not in C89) if the string pointed to does not contain a 0
    before the end of the object: you are still technically responsible
    for passing a string in the parameter position, even if you've told
    it to only copy so many characters out of the string.
    --
    amazon.com's top 8 books about "walter" are Kotzwinkle/ Gundy/ Colman's
    "Walter the Farting Dog"

    Comment

    • Richard Heathfield

      #3
      Re: How to printf 'n' characters from a char* to a file?

      Jonathan said:
      Hi,
      >
      Can you please tell me how can I printf 'n' characters of from a char*
      to a file.
      fprintf(fp, "%.*s\n", n, s);

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

      • Jonathan

        #4
        Re: How to printf 'n' characters from a char* to a file?

        On Jun 5, 3:27 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
        Jonathan said:
        >
        Hi,
        >
        Can you please tell me how can I printf 'n' characters of from a char*
        to a file.
        >
        fprintf(fp, "%.*s\n", n, s);
        >
        --
        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
        Thank you for all the answers.

        Comment

        • Harald van =?UTF-8?b?RMSzaw==?=

          #5
          Re: How to printf 'n' characters from a char* to a file?

          On Thu, 05 Jun 2008 20:21:29 +0000, Walter Roberson wrote:
          printf("%.*s", n, TheCharPointer) ;
          [...]
          Also, the behaviour is not explicitly defined (at least not in
          C89) if the string pointed to does not contain a 0 before the end of the
          object:
          <nit>If the array pointed to does not contain a 0 before the end of the
          object, it's not a string.</nit>
          you are still technically responsible for passing a string in
          the parameter position, even if you've told it to only copy so many
          characters out of the string.
          Really? Could you show what C89 says? I don't have a copy of it, but I'm
          interested because both a draft and text based on the official standard do
          make it clear '\0' is not required, and the text in both is largely
          identical:

          C89 draft:
          "Characters from the array are written up to (but not including) any
          terminating null character; if the precision is specified, no more than
          that many characters are written. If the precision is not specified or is
          greater than the size of the array, the array shall contain a null
          character."

          SUSv2, based on C89:
          "Bytes from the array are written up to (but not including) any
          terminating null byte. If the precision is specified, no more than that
          many bytes are written. If the precision is not specified or is greater
          than the size of the array, the array must contain a null byte."

          I would be very surprised if this text was present in the drafts, dropped
          from the C89 standard, only to be added again afterwards by both SUS and
          C99.

          Comment

          Working...