zoukankan      html  css  js  c++  java
  • snprintf vs sprintf

    #include <stdio.h>
           int printf(const char *format, ...);
           int fprintf(FILE *stream, const char *format, ...);
           int dprintf(int fd, const char *format, ...);
           int sprintf(char *str, const char *format, ...);
           int snprintf(char *str, size_t size, const char *format, ...);
    #include <stdarg.h>
           int vprintf(const char *format, va_list ap);
           int vfprintf(FILE *stream, const char *format, va_list ap);
           int vdprintf(int fd, const char *format, va_list ap);
           int vsprintf(char *str, const char *format, va_list ap);
           int vsnprintf(char *str, size_t size, const char *format, va_list ap);
    

    int snprintf(char *str, size_t n, const char *format, ...);
    函数说明:最多从源串中拷贝n-1个字符到目标串中,然后再在后面加一个0,总共拷贝n个字符。
    snprintf的返回值是欲写入的字符串长度,而不是实际写入的字符串度。

    一、验证存入结果:

    #include<stdio.h>
    #define BUFSIZE 9
    
    void init_buf(char *buf, size_t size);
    void print_buf(char *buf);
    
    int main(){
        char buf[BUFSIZE];
        init_buf(buf, BUFSIZE);
        print_buf(buf);
    
        // hello there! == 12 characters, > BUFSIZE
        init_buf(buf, BUFSIZE);
        snprintf(buf, BUFSIZE, "hello there!");
        print_buf(buf);
    
        // turtle == 6 charaters, < BUFSIZE
        init_buf(buf, BUFSIZE);
        snprintf(buf, BUFSIZE, "turtle");
        print_buf(buf);
    
        // 2222220 == 7 charaters, > 5
        init_buf(buf, BUFSIZE);
        snprintf(buf, 5, "%d", 222222 * 10);
        print_buf(buf);
    
        return 0;
    }
    
    void init_buf(char *buf, size_t size)
    {
        int i;
        for(i=0; i<size; i++){
            buf[i] = i + '0'; // int to char conversion
        }
    }
    
    void print_buf(char *buf)
    {
        int i;
        char c;
        for(i=0; i<BUFSIZE; i++){
            c = buf[i];
            if(c == ''){
                printf("\0");
    
            }
            else{
                printf("%c", buf[i]);
            }
        }
        printf("
    ");
    }
    
    

    The output:

    012345678
    hello th
    turtle78
    22225678

    二、验证返回结果:

    char test[8];
    int ret = snprintf(test,5,"1234567890");
    printf("%d|%s/n",ret,test);

    运行结果为:
    10|1234

    #include<stdio.h>
    #define BUFSIZE 10
    
    int main(){
        char buf[BUFSIZE];
        if(snprintf(buf, BUFSIZE, "hello") >= BUFSIZE){
            printf("%s
    ", buf);
        }
    
        if(snprintf(buf, BUFSIZE, "An extremely long string") >= BUFSIZE){
            printf("buf: %s
    ", buf);
        }
    
        if(snprintf(buf, BUFSIZE, "0%d", 123456789) >= BUFSIZE){
            printf("buf: %s
    ", buf);
        }
    
        return 0;
    }
    

    The output:

    buf: An extrem
    buf: 012345678

    三、应用:

    #include<stdio.h>
    #include<stdlib.h>
    
    int main(){
        int bufSize = 10;
        char *mystr = "This is my string!";
        char *buf = malloc(bufSize);
    
        if(snprintf(buf, bufSize, "%s", mystr) >= bufSize){
            bufSize *= 2;
            printf("Not enough space. Trying %d bytes
    ", bufSize);
            free(buf);
            buf = malloc(bufSize);
    
            if(snprintf(buf, bufSize, "%s", mystr) >= bufSize){
                printf("Still not enough space. Aborting
    ");
                exit(1);
            }
        }
    
        printf("There was enough space!
    ");
        printf("buf: %s
    ", buf);
        return 0;
    }
    
    

    The output:

    Not enough space. Trying 20 bytes
    There was enough space!
    buf: This is my string!

    int sprintf(char *str, const char *format, ...)
    将字符串写入str中,格式为format,不检查是否会溢出
    例如:
    sprintf(s, "%d", 123); //把整数123打印成一个字符串保存在s中
    sprintf(s, "%8x", 4567); //小写16进制,宽度占8个位置,右对齐

    #include <stdio.h>#include <math.h>
    
    int main(){
       char str[80];
    
       sprintf(str, "Value of Pi = %f", M_PI);
       puts(str);
    
       return(0);
    }
    

    The answer is :
    Value of Pi = 3.141593

  • 相关阅读:
    docker 安装ELK
    关于centOS安装配置mysql5.6那点事
    关于centOS安装配置xampp那点事
    PowerDesigner16.5连接Oracle数据库生成E-R图
    Microsoft Visual Studio 中工具箱不显示DevExpress控件的解决办法
    Linux 基础命令
    oracle数据库表空间创建&导入&导出
    weblogic10.3.6漏洞修改方案
    oracle数据库表空间追加数据库文件方法
    转移博客
  • 原文地址:https://www.cnblogs.com/Nice-Boy/p/6735805.html
Copyright © 2011-2022 走看看