1.fprint函数
函数说明:fprintf()会根据参数format 字符串来转换并格式化数据, 然后将结果输出到参数stream 指定的文件中, 直到出现字符串结束('\0')为止。
返回值:关于参数format 字符串的格式请参考printf(). 成功则返回实际输出的字符数, 失败则返回-1, 错误原因存于errno 中.
#include <stdio.h>
int main()
{
int i=1;
char *s="cyf";
fprintf(stdout,"%s,%d",s,i);
return 0;
}
2.vsprintf函数
函数声明:int vfprintf(FILE *stream, char *format, va_list param);
函数说明:发送格式化输出到一个流使用传递给它的参数列表。
返回值:如果成功,返回写入的字符的总数,否则则返回一个负数。
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define debug(...)\
_debug(__FILE__, __LINE__, __VA_ARGS__)
void _debug(const char *file, int line, const char *fmt, ...)
{
va_list argp;
time_t timep;
char *timestr;
struct tm *local;
time(&timep);
local=localtime(&timep);
timestr=asctime(local);
fprintf(stderr,"[%.*s] %s:%d ",(int)(strlen(timestr)-1), timestr, file, line);
va_start(argp,fmt);
vfprintf(stderr, fmt, argp);
fflush(stderr);
va_end(argp);
}
int main()
{
int i=1;
if(i!=2)
{
debug("the number is not %d",i);
}
return 0;
}
自定义调试信息的输出
qDebug( "[模块名称] 调试信息 File:%s, Line:%d", __FILE__, __LINE__ );
这样的修改比较烦人, 而且一不小心会遗漏某个没改的...
2) __FILE__ 宏在预编译时会替换成当前的源文件名
3) __LINE__宏在预编译时会替换成当前的行号
4) __FUNCTION__宏在预编译时会替换成当前的函数名称
3.c语言%.*s是什么
*用来指定宽度,对应一个整数
.(点)与后面的数合起来 是指定必须输出这个宽度,如果所输出的字符串长度大于这个数,则按此宽度输出,如果小于,则输出实际长度
4.memset
函数声明:void *memset(void *s, int ch, size_t n);
用途:为一段内存的每一个字节都赋予ch所代表的值,该值采用ASCII编码。
所属函数库:<memory.h> 或者 <string.h>
#include <stdio.h>
#include <string.h>
int main()
{
int a[10];
memset(a,'\0',sizeof(a));
int i;
for(i=0;i<10;i++)
printf("%d ",a[i]);
return 0;
}
5.sprintf
函数声明:int sprintf(char *str, const char *format, ...)
用途:把格式化的数据写入到某个字符串
#include <stdio.h>
int main()
{
char buff[10];
char *s="this is not ok";
sprintf(buff,"%s %d", s, 10);
printf("%s",buff);
return 0;
}
6.fgets
函数声明:char *fgets(char *s, int n, FILE *stream);
参数:
s:字符行指针,指向存储读入数据的缓冲去的地址
n:从流中读入n-1个字符
stream:指向读取的流
strchr
函数声明:char *strchr(char *str, char ch)
用途:找出在资付出str中第一次出现字符ch的位置,并返回该字符位置的指针,找不到就返回空指针
strstr
#include <stdio.h>
#include <string.h>
#define DEFAUT_FILE "/home/cyf/tsar/conf/tsar.conf"
int main()
{
FILE *fp;
char buff[1024];
if(!(fp=fopen(DEFAUT_FILE,"r")))
{
fprintf(stderr,"%s","error in start");
}
while(fgets(buff,1024,fp))
{
char *temp;
if(temp=strchr(buff,'\n'))
*temp='\0';
if(temp=strchr(buff,'\r'))
*temp='\0';
if(buff[0]=='#')
memset(buff, '\0', 1024);
printf("%s",buff);
}
if(fclose(fp)<0)
fprintf(stderr,"%s","error");
return 0;
}
函数原型:char *strstr( char *str, char * substr );
【参数说明】str为要检索的字符串,substr为要检索的子串。
【返回值】返回字符串str中第一次出现子串substr的地址;如果没有检索到子串,则返回NULL。