Like Tree2Likes
  • 2 Post By anduril462

How to find out the length of a printf before it's printed

This is a discussion on How to find out the length of a printf before it's printed within the C Programming forums, part of the General Programming Boards category; Hi, everyone. I have: printf ("Our data are : %d, %c, %x, %p", myint, mychar, myhex, mypointer); Now, this will ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    126

    How to find out the length of a printf before it's printed

    Hi, everyone. I have:

    printf ("Our data are : %d, %c, %x, %p", myint, mychar, myhex, mypointer);

    Now, this will return the number of characters printed. Is there any way to find out the length of the printed text before it gets printed? Is this the best that can be done?

    Code:
    char szbuffer[100];
    snprintf (szbuffer, 100, "Our data are : %d, %c, %x, %p", myint, mychar, myhex, mypointer);
    strlen (szbuffer);

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Yes.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    You're better off checking the return value of snprintf. Same results, but saves you traversing the string again via strlen.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    50
    what is the function of snprintf??
    is it to store an output as an string??

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    It's a little like print. Google for details.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    50
    thx anduril462

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    126
    The problem is that the return value of snprintf () is poorly documented and inconsistent.

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    C99 compliant snprintf will accept NULL as buffer. and return the number of characters that would have been written.

    Code:
    size_t len = snprintf(NULL,0,""Our data are : %d, %c, %x, %p", myint, mychar, myhex, mypointer);

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    126

    C99, yes

    ISO/IEC 9899:1999 says sprintf returns -1 if the output is truncated, and MSVC does this. So if I need portability, I can't use sprintf's return value.

    Richard

  10. #10
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I'm using snprintf. erm. where did you get that information?
    Upon successful return, these functions return the number of characters printed (not including the trailing '\0' used to end output to strings). The functions snprintf() and vsnprintf() do not write more than size bytes (including the trailing '\0'). If the output was truncated due to this limit then the return value is the number of characters (not including the trailing '\0') which would have been written to the final string if enough space had been available. Thus, a return value of size or more means that the output was truncated. (See also below under NOTES.) If an output error is encountered, a negative value is returned.
    http://www.gnu.org/s/hello/manual/au...rtability.html
    The C99 standard says that if the output array isn't big enough and if no other errors occur, snprintf and vsnprintf truncate the output and return the number of bytes that ought to have been produced. Some older systems return the truncated length (e.g., GNU C Library 2.0.x or IRIX 6.5), some a negative value (e.g., earlier GNU C Library versions), and some the buffer length without truncation (e.g., 32-bit Solaris 7). Also, some buggy older systems ignore the length and overrun the buffer (e.g., 64-bit Solaris 7).
    Forget about MSVC. It's not [C99] standard complaint.
    Last edited by Bayint Naung; 04-08-2011 at 03:14 PM.

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    ISO/IEC 9899:1999 says sprintf returns -1 if the output is truncated, and MSVC does this. So if I need portability, I can't use sprintf's return value.
    sprintf is not the same as snprintf.

    snprintf is one of the most portable ways to do this, as long as the implementation followed the standard.

    There is another way. On windows, you can write anything to NUL, a bit bucket, and the operation will be successful, but the input will be discarded. The Linux version of this is called "/dev/null". I don't know if other operating systems provide this (they certainly don't have to). If what I mentioned is all you care about though, that's portable enough.

    Code:
    long int printed = 0;
    FILE *fnul = fopen(onlinux() ? "/dev/null" : "NUL", "w");
    if (fnul != NULL) {
       printed = fprintf(fnul, "Our data are : %d, %c, %x, %p", myint, mychar, myhex, mypointer);
       fclose(fnul);
    }
    In any case, I wonder what you need this information for, because as far as I know, the only time printf fails to write is if stdout isn't open. Other problems with printf and its arguments will be caught at compile time. Is stdout's state really a concern for you, and why?
    Last edited by whiteflags; 04-08-2011 at 03:20 PM.

  12. #12
    Registered User
    Join Date
    Feb 2011
    Posts
    126

    Portability is important

    None of the examples you've given are as portable as the code I first posted.

  13. #13
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Well only if you are stuck with non-standard library/compilers.
    C99 standard snprintf() function ?
    Last edited by Bayint Naung; 04-08-2011 at 03:26 PM.

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    That was corrected in one of the corrigenda (TC1, TC2 or TC3) to state that it should return the number of characters that would have been written. Most implementations now do this. Of course your portability is only a problem because Microsoft refuses to make their C compiler comply with a 12 year old standard. You could do the following:
    Code:
    #ifdef CRAPPY_OUT_DATED_MS_COMPILER
    #warning "Get a better compiler!"
    // fugly strlen implementation
    #else
    // sexy snprintf(NULL, 0) implementation
    #endif
    Bayint Naung and Salem like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems reading entered race times C
    By loopymoo26 in forum C Programming
    Replies: 12
    Last Post: 05-23-2009, 07:38 AM
  2. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  3. IF CONDITION plese help
    By birumut in forum C Programming
    Replies: 12
    Last Post: 03-06-2009, 08:48 PM
  4. Replies: 2
    Last Post: 03-05-2009, 09:25 AM
  5. Replies: 4
    Last Post: 03-31-2003, 11:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21