• 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

  • 相关阅读:
    【转】myeclipse中连接mysql数据库
    struts2入门示例(hello world)
    【转】MyEclipse第一个Servlet程序
    学习马士兵的struts2/hibernate/spring中遇到的问题及其解决方法
    typeof关键字
    SHLVL--shell终端深度
    stack
    queue
    getopt--parse command line options
    怎样实时判断socket连接状态?
  • 原文地址:https://www.cnblogs.com/Nice-Boy/p/6735805.html
Copyright © 2020-2023  润新知