当前位置: 代码迷 >> 综合 >> sscanf()与sprintf()
  详细解决方案

sscanf()与sprintf()

热度:14   发布时间:2024-02-07 00:59:15.0

①sscanf()

语法:

#include <stdio.h>int sscanf( const char *buffer, const char *format, ... );

函数sscanf()和scanf()类似, 只是输入从buffer(缓冲区)中读取. 

/* sscanf example */
#include <stdio.h>int main ()
{char sentence []="Rudolph is 12 years old";char str [20];int i;sscanf (sentence,"%s %*s %d",str,&i);printf ("%s -> %d\n",str,i);return 0;
}
/*输出:
Rudolph -> 12
*/

语法:

#include <stdio.h>int sprintf( char *buffer, const char *format, ... );

sprintf()函数和printf()类似, 只是把输出发送到buffer(缓冲区)中.

返回值:

成功后,将返回写入的字符总数。
失败时,将返回负数。

/* sprintf example */
#include <stdio.h>int main ()
{char buffer [50];int n, a=5, b=3;n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);printf ("[%s] is a string %d chars long\n",buffer,n);return 0;
}
/*输出
[5 plus 3 is 8] is a string 13 chars long
*/

 

  相关解决方案