#include <stdio.h> // void perror(const char *msg);
#include <string.h> // char *strerror(int errnum);
#include <errno.h> //errno
errno 是错误代码,在 errno.h头文件中;
perror是错误输出函数,输出格式为:msg:errno对应的错误信息(加上一个换行符);
strerror 是通过参数 errnum (就是errno),返回对应的错误信息。
int snprintf(char *str, size_t size, const char *format, ...);
ret = snprintf();
成功 ret = 预写入的字符串长度(不包括'/0'),失败, ret = 负值。
The functions snprintf() and vsnprintf() do not write more than size bytes (including the trailing '/0').
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char str1[10]={0,},str2[10]={0,};
int ret1,ret2;
char *mac="abc";
ret1 = snprintf(str1,3,"%s","abc");
ret2 = snprintf(str2,50,"%s","aaabbbcccddd");
printf("ret1= %d, str1= %s/n",ret1,str1);
printf("ret2= %d, str2= %s/n",ret2,str2);
printf("%d/n",sizeof("abc"));
printf("%d/n",strlen("abc"));
}
/********************/
[andrew@localhost new]$ ./test
ret1= 3, str1= ab
ret2= 12, str2= aaabbbcccddd
4
3
/****************/