snprintf函數使用,snprintf函數

來源:互聯網
上載者:User

snprintf函數使用,snprintf函數

int snprintf(char *restrict buf, size_t n, const char * restrict  format, ...);

函數說明:最多從源串中拷貝n-1個字元到目標串中,然後再在後面加一個0。

函數返回值:若成功則返回寫入的字串長度,若出錯則返回負值,注意,只有當這個返回值是非負的,並且小於n,才表明該字串已被完全寫入。

#include <stdio.h>
#include <stdlib.h>

int main()
{
     char str1[10]={0,};
     snprintf(str1, sizeof(str), "01234567890123456");
     printf("str1=%s/n", str1);
     return 0;
}

結果
str1=012345678


附C++標準說明http://www.cplusplus.com/reference/cstdio/snprintf/

function<cstdio>snprintf
int snprintf ( char * s, size_t n, const char * format, ... );
Write formatted output to sized bufferComposes a string with the same text that would be printed if  format was used on printf, but instead of being printed, the content is stored 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.

A terminating null character is automatically appended after the content written.

After the  format parameter, the function expects at least as many additional arguments as needed for  format.

Parameters
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.
format
C string that contains a format string that follows the same specifications as  format in printf (see printf for details).
...  (additional arguments)
Depending on the  format string, the function may expect a sequence of additional arguments, each containing a value to be used to replace a  format specifier in the  format string (or a pointer to a storage location, for n).
There should be at least as many of these arguments as the number of values specified in the  format specifiers. Additional arguments are ignored by the function.

Return ValueThe 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.

Example
123456789101112131415161718
/* snprintf example */#include <stdio.h>int main (){  char buffer [100];  int cx;  cx = snprintf ( buffer, 100, "The half of %d is %d", 60, 60/2 );  if (cx>=0 && cx<100)      // check returned value    snprintf ( buffer+cx, 100-cx, ", and the half of that is %d.", 60/2/2 );  puts (buffer);  return 0;}
Edit & Run


Output:
The half of 60 is 30, and the half of that is 15.

For more examples on formatting see printf.

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.