zoukankan      html  css  js  c++  java
  • vsnprintf

    http://www.cplusplus.com/reference/cstdio/vsnprintf/

    int vsnprintf (char * s, size_t n, const char * format, va_list arg );
    Write formatted data from variable argument list to sized buffer

    Composes a string with the same text that would be printed if format was used on printf, but using the elements in the variable argument list identified by arg instead of additional function arguments and storing the resulting content as a C string in the buffer pointed by s (taking n as the maximum buffer capacity to fill).

    If the resulting string would be longer than n-1 characters, the remaining characters are discarded and not stored, but counted for the value returned by the function.

    Internally, the function retrieves arguments from the list identified by arg as if va_arg was used on it, and thus the state ofarg is likely to be altered by the call.

    In any case, arg should have been initialized by va_start at some point before the call, and it is expected to be released byva_end at some point after the call.

     s:  Pointer to a buffer where the resulting C-string is stored.
    The buffer should have a size of at least n characters.

    n:  Maximum number of bytes to be used in the buffer.
    The generated string has a length of at most n-1, leaving space for the additional terminating null character.size_t  is an unsigned integral type.formatC string that contains a format string that follows the same specifications as 

    format :  in printf (see printf for details).

    argA :  value identifying a variable arguments list initialized with va_start.va_list is a special type defined in <cstdarg>.

    Return Value

    The number of characters that would have been written if n had been sufficiently large, not counting the terminating null character.
    If an encoding error occurs, a negative number is returned.
    Notice that only when this returned value is non-negative and less than n, the string has been completely written.

    /* vsnprintf example */
    #include <stdio.h>
    #include <stdarg.h>
    
    void PrintFError ( const char * format, ... )
    {
      char buffer[256];
      va_list args;
      va_start (args, format);
      vsnprintf (buffer,256,format, args);
      perror (buffer);//printf("%s",buffer);
    va_end (args);
    }
    
    int main ()
    {
       FILE * pFile;
       char szFileName[]="myfile.txt";
    
       pFile = fopen (szFileName,"r");
       if (pFile == NULL)
         PrintFError ("Error opening '%s'",szFileName);
       else
       {
         // file successfully open
         fclose (pFile);
       }
       return 0;
    }

    in eclipse with cygwin, compile get warning: implicit declaration of function ‘vsnprintf’.

    http://cboard.cprogramming.com/c-programming/90483-snprintf-warning-error.html

    "snprintf was not part of C90, it's a C99 addition or an extension to C90 available to certain compilers.

    It looks like you may be using some variant of gcc, so try adding the flag -std=c99."

    the above answer is right, in my make file, change c89 to c99, problem solved. also, just delete the origninate CPPUTEST_CFLAGS += -std=c89 also works. makefile is useful.

    CPPUTEST_CFLAGS += -std=c99


    to better understand , here is an application of snprintf.

    static void failWhenNotAllExpectationsUsed(void)
    {
        char format[] = "Expected %d reads/writes but got %d";
        char message[sizeof format + 5 + 5];
    
        if (getExpectationCount == setExpectationCount)
            return;
    
        snprintf(message, sizeof message, format, setExpectationCount,
                getExpectationCount);
        fail(message);
    }
    
    static void fail(char *message )
    {
        failureAlreadyReported = 1;
        FAIL_TEXT_C(message);
    }
  • 相关阅读:
    图表算法—有向图
    图表算法—无向图
    搜索算法—哈希表
    红黑树的删除
    搜索算法—红黑树
    搜索算法—二叉搜索树
    排序算法—堆排序
    快速排序改进——3区快速排序(3-way quicksort)
    数论——约数:算数基本定理及推论,欧几里得算法
    数论——乘法逆元(快速幂求法)及模运算
  • 原文地址:https://www.cnblogs.com/aprilapril/p/5242513.html
Copyright © 2011-2022 走看看