Chinaunix首页 | 论坛 | 博客
  • 博客访问: 287003
  • 博文数量: 70
  • 博客积分: 485
  • 博客等级: 下士
  • 技术积分: 632
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-25 08:55
文章分类

全部博文(70)

文章存档

2014年(47)

2013年(1)

2012年(22)

我的朋友

分类: C/C++

2014-04-22 14:31:23

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

函数说明:最多从源串中拷贝n-1个字符到目标串中,然后再在后面加一个'\0'。所以如果目标串的大小为n 的话,将不会溢出。

函数返回值:若成功则返回欲写入的字符串长度,若出错则返回负值。


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4.      char s[10] = {0};
  5.      snprintf(s, sizeof(s), "01234567");
  6.      printf("s[%s]\n", s);
  7.      return 0;
  8. }
输出结果:s[01234567]
在看一个反面例子:

点击(此处)折叠或打开

  1. #include <stdio.h>
  2.  
  3.  int main(void)
  4.  {
  5.      char s[10] = {0};
  6.      snprintf(s, sizeof(s), "0123456701234567");
  7.      printf("s[%s]\n", s);
  8.      return 0;
  9.   }
输出结果:s[012345670]

int fprintf( FILE *stream, const char *format, ... );
fprintf()函数根据指定的format(格式)发送信息(参数)到由stream(流)指定的文件.因此fprintf()可以使得信息输出到指定的文件.

点击(此处)折叠或打开

  1. #include <stdio.h>
  2.   
  3.   int main(void)
  4.   {
  5.       char *s="0123456";
  6.       fprintf(stdout,"s:[%s]\n", s);
  7.       return 0;
  8.   }

结果:s:[0123456]


阅读(932) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~