• 实现一个scnprinf


    #include <stdio.h>
    #include <stdarg.h>
    
    /* 该函数ret = min(size, 实际写入长度) - 1,即ret永远小于size
     * scnprintf(NULL, ...) -> segment err
     * scnprintf(str, 0, ...) -> -1
     * scnprintf(str, 1, ...) -> 0
     * scnprintf(str, size, "%s", NULL) <==> scnprintf(str, size, "%s", "(null)");
     */
    static inline int scnprintf(char *str, size_t size, const char *format, ...)
    {
        va_list ap;
        int len;
    
        va_start(ap, format);
        len = vsnprintf(str, size, format, ap);
        va_end(ap);
    
        if(len > -1 && len >= size)
            len = size - 1;
    
        return len;
    }
    
    int main(int argc, char **argv)
    {
        int write_len = 0, tmp_len;
        char argbuf[16] = {0};
        /*
        for(int i = 1, left_len = sizeof(argbuf); i < argc; i++)
            if((tmp_len = scnprintf(argbuf + write_len, sizeof(argbuf) - write_len, "%s", argv[i])) > 0){
                write_len += tmp_len;
            }else{
                break; // 一般情况下,不会进入这里, 除非snprintf转换失败
            }
        */
    
        for(int i = 1, left_len = sizeof(argbuf); i < argc; i++)
            write_len += scnprintf(argbuf + write_len, sizeof(argbuf) - write_len, "%s", argv[i]);
        /*  argv很长时,必有sizeof(argbuf) - write_len == 1,
         *  此时,下次调用scnprintf返回0,之后的argv[i]空转,不会再拼装任何字符到argbuf
         */
    
        printf("write_len =%d, argbug=%s
    ", write_len, argbuf);
            
        return 0;
    }   
    
  • 相关阅读:
    RESTful API 设计指南
    浅析JS中的模块规范(CommonJS,AMD,CMD)
    Gitbucket—快速建立自己的Github
    单点登录详解
    Java常用类--处理日期
    Java常用类--数字常用类
    java常用类--字符串
    java常用类--系统相关
    java常用类--与用户互动
    设置PATH和CLASSPATH
  • 原文地址:https://www.cnblogs.com/mathzzz/p/3141529.html
Copyright © 2020-2023  润新知