How do I use strlen() on a formatted string?

前端 未结 2 1446
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-22 00:42

I\'d like to write a wrapper function for the mvwprint/mvwchgat ncurses functions which prints the message in the specified window and then changes its attribut

相关标签:
2条回答
  • 2021-01-22 00:54

    In C99 or C11, you can use a line like this:

    length = snprintf(NULL, 0, format_string, args);
    

    From the manual of snprintf (emphasis mine):

    The functions snprintf() and vsnprintf() do not write more than size bytes (including the terminating null byte ('\0')). If the output was truncated due to this limit then the return value is the number of characters (excluding the terminating null byte) 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.

    Since we are giving snprintf 0 as the size, then the output is always truncated and the output of snprintf would be the number of characters that would have been written, which is basically the length of the string.

    In C89, you don't have snprintf. A workaround is to create a temporary file, or if you are in *nix open /dev/null and write something like this:

    FILE *throw_away = fopen("/dev/null", "w"); /* On windows should be "NUL" but I haven't tested */
    if (throw_away)
    {
        fprintf(throw_away, "<format goes here>%n", <args go here>, &length);
        fclose(throw_away);
    } /* else, try opening a temporary file */
    
    0 讨论(0)
  • 2021-01-22 01:09

    You can't know in advance how long your string will be:

    printf("abc%d", 0); //4 chars
    printf("abc%d", 111111111);//12 chars
    

    All with the same format string.

    The only sure way is to sprintf the text in question into a buffer, strlen(buffer) the result and printf("%s", buffer); the result to screen.

    This solution avoids double formatting at the cost of allocating long enough buffer.

    0 讨论(0)
提交回复
热议问题