利用可变参数,将调试信息写入文件log.txt中

来源:互联网 发布:哪些女装淘宝店铺推荐 编辑:程序博客网 时间:2024/06/11 16:18
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


int print_file(void * fmt,...)
{
va_list ap;
char buffer[100] = "";


va_start(ap, fmt);
vsprintf(buffer, fmt, ap);
va_end(ap);


int fd;
fd = open("/log.txt",O_RDWR|O_CREAT|O_APPEND);
if(fd < 0) {
printf("open fail\n");
return ;
}


// lseek(fd, 0, SEEK_END);
write(fd, buffer, strlen(buffer));


close(fd);


return 0;


}


void main()
{
print_file("%d %s  33", 4, "hello world");


}
0 0
原创粉丝点击