Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2118001
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: LINUX

2015-07-15 14:17:16

一. 常用
1. 自定义打印调试信息
  1. a.在头文件中,定义如下:
  2. #ifdef    DEBUG
  3. #define dbmsg(fmt, args ...) printf("%s:%s[%d]: "fmt"\n", __FILE__,__FUNCTION__, __LINE__,##args)
  4. #else
  5. #define dbmsg(fmt,args...)
  6. #endif
  7. b. 需要用到打印调试时: dbmsg("print debug info!\n");
  8. c. 在Makefile中加上: CFLAGS= -DDEBUG
  9. 或者直接在gcc 中: gcc -DDEBUG -g -o foo foo.c
1.1 打印相关
a. int64_t 的打印
printf("%"PRId64"\n", arg);  直接用%d 或 %lld都会有警告
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <inttypes.h>     //加上这个头文件
  4.  
  5. int main ( int argc, char *argv[] )
  6. {
  7.     int64_t pts = 123;
  8.     printf("%"PRId64"\n", pts);   //跨平台的方法加印
  9.     return EXIT_SUCCESS;
  10. }

2. 时间相关
2.1返回当前时间的分钟
  1. #include <time.h>
  2. static int get_current_minute()
  3. {
  4.     time_t now;
  5.     struct tm *current_time;
  6.     int last_minute = 0;
  7.     time(&now);
  8.     current_time = localtime(&now);
  9.     asctime(current_time);
  10.     last_minute = current_time->tm_min;
  11.     return last_minute;
  12. }
2.2 将当前时间转为long int,精确到ms
  1. #include <sys/time.h>
  2. #include <stdlib.h>
  3. long int get_now_ms_time()
  4. {
  5.     struct timeval start;
  6.     long int now_time;
  7.     gettimeofday(&start, NULL);
  8.     now_time = start.tv_sec*1000+ start.tv_usec/1000;
  9.     printf("cong add: now_time=%ld\n",now_time);
  10.     return now_time;
  11. }
这样获取两次usec之后做差就可以知道耗时了
2.3 while超时避免while死循环
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/time.h>
  4. #include <stdlib.h>

  5. #define SPI_READ_TIMEOUT 5000 //ms

  6. long int get_now_ms_time()
  7. {
  8.         struct timeval tm;
  9.         long int now_time;
  10.         gettimeofday(&tm, NULL);
  11.         now_time = tm.tv_sec*1000 + tm.tv_usec/1000;
  12.         //printf("cong add: now_time=%ld\n",now_time);
  13.         return now_time;
  14. }

  15. int main ( int argc, char *argv[] )
  16. {
  17.         long int old_time = get_now_ms_time();
  18.         long int now_time;
  19.         while(1)
  20.         {
  21.                 usleep(1000);
  22.                 now_time = get_now_ms_time();
  23.                 if( (now_time - old_time) > SPI_READ_TIMEOUT )
  24.                 {
  25.                         printf("spi read time out\n");
  26.                         break;
  27.                 }
  28.         }
  29.         printf("now return \n");
  30.         return EXIT_SUCCESS;
  31. }
3.大小端测试
  1. //0-->big_endian 1-->small endian
  2. int checkSystem()
  3. {
  4.     union check
  5.     {
  6.         int i;
  7.         char ch;
  8.     }c;
  9.     c.i = 1;
  10.     return (c.ch == 1);
  11. }
4. 回调函数带参数
  1. int add(int a, int b)
  2. {
  3.     return a + b;
  4. }

  5. typedef int (*PTRADD)(int, int);

  6. int newAdd(int a, int b, PTRADD p)
  7. {
  8.     return p(a, b);
  9. }

  10. int main()
  11. {
  12.     PTRADD p = add;
  13.     int sum = newAdd(3, 3, p);
  14.     printf("sum =%d\n", sum);

  15.     return 0;
  16. }
5.写文件
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. static void write_to_log(char* buf, int size)
  6. {
  7.     int temp_fd;
  8.     int ret;
  9.     temp_fd = open("temp.log", O_CREAT|O_RDWR, S_IRWXU|S_IRWXG|S_IRWXO);
  10.     if(temp_fd < 0)
  11.         perror("file not open!\n");
  12.     lseek(temp_fd,0,SEEK_END);
  13.     if((ret= write(temp_fd, buf, size)) != size)
  14.     {
  15.         perror("file writen error!\n");
  16.         close(temp_fd);
  17.     }
  18.     close(temp_fd);
  19.     return ;
  20. }
6.EXIT_SUCCESS与EXIT_FAILURE的头文件
使用时需包含 stdlib.h




二.字符串相关
2.1 将 ”0102030405“这个字符串转为数字12345
字符串长度必为偶数,这样才能将两个字符作为16进制转为数字
  1. int atoi_t(char* src)
  2. {
  3.     char l,h,ret;
  4.     //dbmsg("src[0]=%c,src[1]=%c", src[0], src[1]);
  5.     if((src[0]>=0x30) && (src[0]<=0x39))
  6.     {
  7.         h = src[0]-'0';
  8.         //printf("h=%d\n", h);
  9.     }else if( (src[0]>=0x41)&&(src[0]<=0x46))
  10.     {
  11.         h = src[0]-'A'+10;
  12.         //printf("h=%d\n", h);
  13.     }

  14.     if((src[1]>=0x30) && (src[1]<=0x39))
  15.     {
  16.         l = src[1]-'0';
  17.         //printf("l=%d\n", l);
  18.     }else if( (src[1]>=0x41)&&(src[1]<=0x46))
  19.     {
  20.         l = src[1]-'A'+10;
  21.         //printf("l=%d\n", l);
  22.     }
  23.     return (h<<4|l);
  24. }

  25. int hex2char(unsigned char* dst, unsigned char* src, int cnt)
  26. {
  27.    int i=0;
  28.    int num=0;
  29.    dbmsg("cnt=%d", cnt);
  30.    if(cnt % 2)
  31.    {
  32.        dbmsg("can't be odd number");
  33.        return -1;
  34.    }
  35.    for(i=0; i<cnt; i+=2)
  36.    {
  37.         dst[num++] = atoi_t((char*)(src+i));
  38.    }
  39. #if 0
  40.    for(i=0; i<num; i++)
  41.    {
  42.        printf("%d=0x%x ", i, dst[i]);
  43.    }
  44.    printf("\n");
  45. #endif
  46.    return num;
  47. }
2.2 将buf:0x12 0x34 0x56转为字符串123456
  1. int char2hex(unsigned char* dst, unsigned char* src, int cnt)
  2. {
  3.     int i;
  4.     if(cnt > 512)
  5.     {
  6.         dbmsg("char2hex too large");
  7.         return -1;
  8.     }
  9.     for(i=0; i<cnt; i++)
  10.       snprintf((char*)dst+2*i, 1024, "%02X",*(src+i));

  11.     dst[2*cnt] = '\0';
  12.     return 0;
  13. }
2.3 按字符串中的逗号过滤,返回指向最后一个逗号下一个字符的首地址
  1. char* filter_string(const char* src)
  2. {
  3.     char* tmp;
  4.     char* dst = (char*)src;
  5.     while(1)
  6.     {
  7.         tmp = strstr(dst, ",");
  8.         if(NULL == tmp)
  9.             break;
  10.         tmp ++;
  11.         dst = tmp;
  12.     }
  13.     dbmsg("dst=%s", dst);
  14.     return dst;
  15. }
例如src=”+EIPRUDP: 192.168.1.2,9527,1,0,00010203040506“,按照字符串中的逗号过滤
则dst=00010203040506


三.API使用
1.mempcpy使用
  1. [问题]warning: incompatible implicit declaration of built-in function ‘mempcpy’ [enabled by default]
  2. [解决]将#define _GNU_SOURCE放在头文件的最开始,当然别忘了#include <string.h>
2. open
  1. open 不能 带O_DIRECT的解决方法
  2. 解决方法在include 之前添加
  3. #define _GNU_SOURCE




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