valist、vsnprintf、snprintf、localtime分析

今天写一个日志类,不过目前还有bug。condition的destroy不知道怎么的死锁了,之前测试很好的,并入其他模块就不能工作了,我得想办法调试一下,毕竟多线程程序不好调试。

先来分析一下今天用的这几个函数,其实以前看libevent,muduo这些函数都出现过,不过学了又忘了,这次总结一下。


1.valist

C语言标准库中头文件stdarg.h声明了一组能够变长参数的宏。主要有:

1、va_list 用来声明一个表示参数表中各个参数的变量。
2、va_start 初始化一个指针来指向变长参数列表的头一个变量(注意,...只能出现在参数表的最后)
3、va_arg每次调用时都会返回当前指针指向的变量,并将指针挪至下一个位置,参数的类型需要在这个调用的第二个参数来指定,va_arg也是根据这个参数来判断偏移的距离。
4、va_end需要在函数最后调用,来进行一些清理工作。

示例:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>

void print(const char* format, ...)
{
    va_list vlist;
    va_start(vlist, format);

    char buf[1024];
    memset(buf, 0, sizeof(buf));

    vsprintf(buf, format, vlist);
    printf("%s\n", buf);

    va_end(vlist);
}

int main()
{
    const char *s1 = "hello,";
    const char *s2 = "world.";
    print("%s%s", s1, s2); 

    return 0;
}
在本例中,便可输出"hello,world."


2.snprintf

snprintf(),为函数原型int snprintf(char *str, size_t size, const char *format, ...)。
将可变个参数(...)按照format格式化成字符串,然后将其复制到str中
(1) 如果格式化后的字符串长度 < size,则将此字符串全部复制到str中,并给其后添加一个字符串结束符('\0');
(2) 如果格式化后的字符串长度 >= size,则只将其中的(size-1)个字符复制到str中,并给其后添加一个字符串结束符('\0'),返回值为欲写入的字符串长度。

所以说,valist和snprintf是天生一对,天衣无缝的配合。


3.vsnprintf和snprintf是C语言printf家族函数成员,相关函数列表如下:

#include <stdio.h>
       int printf(const char *format, ...); //输出到标准输出
       int fprintf(FILE *stream, const char *format, ...); //输出到文件
       int sprintf(char *str, const char *format, ...); //输出到字符串str中
       int snprintf(char *str, size_t size, const char *format, ...); //按size大小输出到字符串str中
  
以下函数功能与上面的一一对应相同,只是在函数调用时,把上面的...对应的一个个变量用va_list调用所替代。在函数调用前ap要通过va_start()宏来动态获取。
#include <stdarg.h>
       int vprintf(const char *format, va_list ap);
       int vfprintf(FILE *stream, const char *format, va_list ap);
       int vsprintf(char *str, const char *format, va_list ap);
       int vsnprintf(char *str, size_t size, const char *format, va_list ap);


例:通过vsnprintf()实现snprintf()功能:

#include <stdio.h>
#include <stdarg.h>
int my_snprintf(char *s, int size, const char *fmt, ...) //该自定义函数,与系统提供的snprintf()函数相同。
{
    va_list ap;
    int n=0;
    va_start(ap, fmt); //获得可变参数列表
    n=vsnprintf (s, size, fmt, ap); //写入字符串s
    va_end(ap); //释放资源
    return n; //返回写入的字符个数
}
int main() {
    char str[1024];
    my_snprintf( str, sizeof(str), "%d,%d,%d,%d",5,6,7,8);
    printf("%s\n",str);
    return 0;
}


4.localtime函数

说localtime函数之前先说一下gettimeofday()函数,该函数取得1970年1月1日零点到当前时间的时间戳:
 int gettimeofday(struct timeval *tv, struct timezone *tz);
一般我们只用第一个参数tv获取秒和微秒的时间戳,然后将秒*1000*1000转化成微秒并加上微秒,即为1970年1月1日零点到当前的微秒数。
第二个参数用来存储时区,一般不用。
  struct timeval {
               time_t      tv_sec;     /* seconds */
               suseconds_t tv_usec;    /* microseconds */
           };
gettimeoufday()函数一般用法:
int64_t get_current_timestamp()
{   
    struct timeval now = {0, 0}; 
    gettimeofday(&now, NULL);
    return now.tv_sec * 1000 * 1000 + now.tv_usec;
}
这样就的到了时间戳。

下面是结合localtime函数转化成供人类阅读的时间形式:

#include <iostream>
#include <time.h>
#include <sys/time.h>
using namespace std;

int main()
{
    struct timeval now = {0, 0}; 
    gettimeofday(&now, NULL);
    time_t t = now.tv_sec;
    struct tm* sys_tm = localtime(&t);
    struct tm my_tm = *sys_tm;

    

    return 0;
}

用GDB即可看到:



5.strchr()函数:查找某一字符在字符串中最后一次出现的位置

比如在   "./path"   中查找'/',这样他就会返回'/'所在位置的下标p,我们用p+1,就可以的到path名称了。


6.fstream

使用如下组合来打开文件,并检测是否真的打开。

记住:使用fstream一定要包含头文件#include <fstream>,只有iostream是不管用的。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值