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的返回值是欲寫入的字符串長度,而不是實際寫入的字符串度。函數

1、驗證存入結果:spa

#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 == '\0'){
            printf("\\0");

        }
        else{
            printf("%c", buf[i]);
        }
    }
    printf("\n");
}

The output:code

012345678
hello th\0
turtle\078
2222\05678orm

2、驗證返回結果:rem

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

運行結果爲:
10|1234string

#include<stdio.h>
#define BUFSIZE 10

int main(){
    char buf[BUFSIZE];
    if(snprintf(buf, BUFSIZE, "hello") >= BUFSIZE){
        printf("%s\n", buf);
    }

    if(snprintf(buf, BUFSIZE, "An extremely long string") >= BUFSIZE){
        printf("buf: %s\n", buf);
    }

    if(snprintf(buf, BUFSIZE, "0%d", 123456789) >= BUFSIZE){
        printf("buf: %s\n", buf);
    }

    return 0;
}

The output:it

buf: An extrem
buf: 012345678io

3、應用:form

#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\n", bufSize);
        free(buf);
        buf = malloc(bufSize);

        if(snprintf(buf, bufSize, "%s", mystr) >= bufSize){
            printf("Still not enough space. Aborting\n");
            exit(1);
        }
    }

    printf("There was enough space!\n");
    printf("buf: %s\n", 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

相關文章
相關標籤/搜索