一. 常用
1. 自定义打印调试信息
-
a.在头文件中,定义如下:
-
#ifdef DEBUG
-
#define dbmsg(fmt, args ...) printf("%s:%s[%d]: "fmt"\n", __FILE__,__FUNCTION__, __LINE__,##args)
-
#else
-
#define dbmsg(fmt,args...)
-
#endif
-
b. 需要用到打印调试时: dbmsg("print debug info!\n");
-
c. 在Makefile中加上: CFLAGS= -DDEBUG
-
或者直接在gcc 中: gcc -DDEBUG -g -o foo foo.c
1.1 打印相关
a. int64_t 的打印
printf("%"PRId64"\n", arg); 直接用%d 或 %lld都会有警告
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <inttypes.h> //加上这个头文件
-
-
int main ( int argc, char *argv[] )
-
{
-
int64_t pts = 123;
-
printf("%"PRId64"\n", pts); //跨平台的方法加印
-
return EXIT_SUCCESS;
-
}
2. 时间相关
2.1返回当前时间的分钟
-
#include <time.h>
-
static int get_current_minute()
-
{
-
time_t now;
-
struct tm *current_time;
-
int last_minute = 0;
-
time(&now);
-
current_time = localtime(&now);
-
asctime(current_time);
-
last_minute = current_time->tm_min;
-
return last_minute;
-
}
2.2 将当前时间转为long int,精确到ms
-
#include <sys/time.h>
-
#include <stdlib.h>
-
long int get_now_ms_time()
-
{
-
struct timeval start;
-
long int now_time;
-
gettimeofday(&start, NULL);
-
now_time = start.tv_sec*1000+ start.tv_usec/1000;
-
printf("cong add: now_time=%ld\n",now_time);
-
return now_time;
-
}
这样获取两次usec之后做差就可以知道耗时了
2.3 while超时避免while死循环
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <sys/time.h>
-
#include <stdlib.h>
-
-
#define SPI_READ_TIMEOUT 5000 //ms
-
-
long int get_now_ms_time()
-
{
-
struct timeval tm;
-
long int now_time;
-
gettimeofday(&tm, NULL);
-
now_time = tm.tv_sec*1000 + tm.tv_usec/1000;
-
//printf("cong add: now_time=%ld\n",now_time);
-
return now_time;
-
}
-
-
int main ( int argc, char *argv[] )
-
{
-
long int old_time = get_now_ms_time();
-
long int now_time;
-
while(1)
-
{
-
usleep(1000);
-
now_time = get_now_ms_time();
-
if( (now_time - old_time) > SPI_READ_TIMEOUT )
-
{
-
printf("spi read time out\n");
-
break;
-
}
-
}
-
printf("now return \n");
-
return EXIT_SUCCESS;
-
}
3.大小端测试
-
//0-->big_endian 1-->small endian
-
int checkSystem()
-
{
-
union check
-
{
-
int i;
-
char ch;
-
}c;
-
c.i = 1;
-
return (c.ch == 1);
-
}
4. 回调函数带参数
-
int add(int a, int b)
-
{
-
return a + b;
-
}
-
-
typedef int (*PTRADD)(int, int);
-
-
int newAdd(int a, int b, PTRADD p)
-
{
-
return p(a, b);
-
}
-
-
int main()
-
{
-
PTRADD p = add;
-
int sum = newAdd(3, 3, p);
-
printf("sum =%d\n", sum);
-
-
return 0;
-
}
5.写文件
-
#include <sys/types.h>
-
#include <sys/stat.h>
-
#include <fcntl.h>
-
#include <unistd.h>
-
static void write_to_log(char* buf, int size)
-
{
-
int temp_fd;
-
int ret;
-
temp_fd = open("temp.log", O_CREAT|O_RDWR, S_IRWXU|S_IRWXG|S_IRWXO);
-
if(temp_fd < 0)
-
perror("file not open!\n");
-
lseek(temp_fd,0,SEEK_END);
-
if((ret= write(temp_fd, buf, size)) != size)
-
{
-
perror("file writen error!\n");
-
close(temp_fd);
-
}
-
close(temp_fd);
-
return ;
-
}
6.EXIT_SUCCESS与EXIT_FAILURE的头文件
使用时需包含 stdlib.h
二.字符串相关
2.1 将 ”0102030405“这个字符串转为数字12345
字符串长度必为偶数,这样才能将两个字符作为16进制转为数字
-
int atoi_t(char* src)
-
{
-
char l,h,ret;
-
//dbmsg("src[0]=%c,src[1]=%c", src[0], src[1]);
-
if((src[0]>=0x30) && (src[0]<=0x39))
-
{
-
h = src[0]-'0';
-
//printf("h=%d\n", h);
-
}else if( (src[0]>=0x41)&&(src[0]<=0x46))
-
{
-
h = src[0]-'A'+10;
-
//printf("h=%d\n", h);
-
}
-
-
if((src[1]>=0x30) && (src[1]<=0x39))
-
{
-
l = src[1]-'0';
-
//printf("l=%d\n", l);
-
}else if( (src[1]>=0x41)&&(src[1]<=0x46))
-
{
-
l = src[1]-'A'+10;
-
//printf("l=%d\n", l);
-
}
-
return (h<<4|l);
-
}
-
-
int hex2char(unsigned char* dst, unsigned char* src, int cnt)
-
{
-
int i=0;
-
int num=0;
-
dbmsg("cnt=%d", cnt);
-
if(cnt % 2)
-
{
-
dbmsg("can't be odd number");
-
return -1;
-
}
-
for(i=0; i<cnt; i+=2)
-
{
-
dst[num++] = atoi_t((char*)(src+i));
-
}
-
#if 0
-
for(i=0; i<num; i++)
-
{
-
printf("%d=0x%x ", i, dst[i]);
-
}
-
printf("\n");
-
#endif
-
return num;
-
}
2.2 将buf:0x12 0x34 0x56转为字符串123456
-
int char2hex(unsigned char* dst, unsigned char* src, int cnt)
-
{
-
int i;
-
if(cnt > 512)
-
{
-
dbmsg("char2hex too large");
-
return -1;
-
}
-
for(i=0; i<cnt; i++)
-
snprintf((char*)dst+2*i, 1024, "%02X",*(src+i));
-
-
dst[2*cnt] = '\0';
-
return 0;
-
}
2.3 按字符串中的逗号过滤,返回指向最后一个逗号下一个字符的首地址
-
char* filter_string(const char* src)
-
{
-
char* tmp;
-
char* dst = (char*)src;
-
while(1)
-
{
-
tmp = strstr(dst, ",");
-
if(NULL == tmp)
-
break;
-
tmp ++;
-
dst = tmp;
-
}
-
dbmsg("dst=%s", dst);
-
return dst;
-
}
例如src=”+EIPRUDP: 192.168.1.2,9527,1,0,00010203040506“,按照字符串中的逗号过滤
则dst=00010203040506
三.API使用
1.mempcpy使用
-
[问题]warning: incompatible implicit declaration of built-in function ‘mempcpy’ [enabled by default]
-
[解决]将#define _GNU_SOURCE放在头文件的最开始,当然别忘了#include <string.h>
2. open
-
open 不能 带O_DIRECT的解决方法
-
解决方法在include 之前添加
-
#define _GNU_SOURCE
阅读(1306) | 评论(0) | 转发(0) |