gps数据格式为:$GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,150706,,,A*
/**********************************************************************
*版权所有 (C)2015, Wuyq。
*
*文件名称: GetValueFromStr.c
*内容摘要:用于演示从gps数据字符串中获取相应的内容
*其它说明:无
*当前版本: V1.0
*作 者: wuyq
*完成日期: 20150210
*
* 版本 修改时间 修改人 修改内容
********************************************************************
* V1.0 20150210 wuyq 创建
**********************************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//字段最大长度
#define MAX_RET_BUF_LEN (1024)
//数据类型
typedef unsigned char UINT8;
typedef unsigned short int UINT16;
typedef unsigned int UINT32;
typedef signed int INT32;
typedef unsigned char BOOL;
#define TRUE (BOOL)1
#define FALSE (BOOL)0
/**********************************************************************
*功能描述:获取字符串中某一个字段的数据
*输入参数: iSerialNum-字段编号(为正整数)
pSourceStr-源字符串
pDstStr-目的字符串(提取的数据的存放位置)
cIsolater-源字符串中字段的分隔符
iDstStrSize-目的字符串的长度
*输出参数:无
*返回值: TRUE-成功 FALSE-失败
*其它说明:无
*修改日期 版本号 修改人 修改内容
* --------------------------------------------------------------
* 20150210 V1.0 wuyq 创建
***********************************************************************/
BOOL GetValueFromStr(UINT16 iSerialNum, UINT8 *pSourceStr, UINT8 *pDstStr, UINT8 cIsolater, UINT32 iDstStrSize)
{
UINT8 *pStrBegin = NULL;
UINT8 *pStrEnd = NULL;
UINT8 szRetBuf[MAX_RET_BUF_LEN] = { 0 }; //截取出的字符串放入该数组中
UINT8 *pUINT8 = NULL;
UINT16 *pUINT16 = NULL;
UINT32 *pUINT32 = NULL;
UINT32 iFieldLen = 0; //用于表示每个字段的实际长度
if (pSourceStr == NULL) //对输入指针的异常情况进行判断
{
return FALSE;
}
//字段首
pStrBegin = pSourceStr;
while (--iSerialNum != 0)
{
printf("wo zhixing le\n");
pStrBegin = strchr(pStrBegin, cIsolater);
if (pStrBegin == NULL)
{
return FALSE;
}
pStrBegin++;
}
//字段尾
pStrEnd = strchr(pStrBegin, cIsolater);
if (pStrEnd == NULL)
{
return FALSE;
}
iFieldLen = (UINT16)(pStrEnd - pStrBegin);
if (iFieldLen >= MAX_RET_BUF_LEN) //进行异常保护, 防止每个字段的值过长
{
iFieldLen = MAX_RET_BUF_LEN - 1;
}
memcpy(szRetBuf, pStrBegin, iFieldLen);
strncpy(pDstStr, szRetBuf, iDstStrSize);
return TRUE;
}
/****************************************************************
*功能描述: 主函数 *
*输入参数: 无 *
*输出参数: 无 *
*返回值 :无 *
*其他说明: 无 *
*修改日期 版本号 修改人 修改内容
* -------------------------------------------------------------------------------
* 20150210 V1.0 wuyq 创建
****************************************************************/
void main()
{
FILE *hFile = NULL; //文件句柄指针
UINT8 szContentLine[1024] = { 0 }; //用于存放从文件中独到的每条记录
UINT8 szDes[100] = { 0 };//接收分离出的数据
//打开文件
hFile = fopen("gpsdata.txt", "r");
if (!hFile) //打开失败
{
printf("Open gpsdata.txt failed!\n");
return -1; //异常退出
}
while (NULL != fgets(szContentLine, sizeof(szContentLine), hFile))//不断地读取文件
{
printf("szContentLine=[%s]\n", szContentLine);//打印没次读到一行的数据内容
if (TRUE != GetValueFromStr(1, szContentLine, szDes, ',', sizeof(szDes)))
{
printf("获取信息1失败.\n");
return -1;
}
printf("szDes=[%s]\n", szDes);
if (TRUE != GetValueFromStr(2, szContentLine, szDes, ',', sizeof(szDes)))
{
printf("获取信息2失败.\n");
return -1;
}
printf("szDes=[%s]\n", szDes);
// ...
}
fclose(hFile);
system("pause");
}
字符串解析parse_string获取系统内存,cpu使用情况等。
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* 字符串解析*/
/*打开的文件,文件中要查找的字符串,返回数据的地址首地址*/
char *parse_string(char *file, char *string)
{
int fd;
char pbuf[1024 * 10];
int nread;
char *ptmp;
char *end;
static char start[100];
if (!file || !string)
{
return NULL;
}
/* 打开文件*/
fd = open(file, O_RDONLY);
if (fd < 0)
{
printf("open %s file error!\n", file);
return NULL;
}
/*将文件读入缓冲区中*/
nread = read(fd, pbuf, sizeof(pbuf));//将文件中的内容,存放到pbuf中
if (nread < 0)
{
printf("read %s file error!", file);
return NULL;
}
/* 查找文件中的字符串*/
ptmp = strstr(pbuf, string);
ptmp += strlen(string);//跳过查找到的字符串位置
while ((*ptmp == ' ') || (*ptmp == ':') || (*ptmp == '\t'))
{
ptmp++;
}
//start[0] = *ptmp;//debug
//printf("start:[%c]\n", *start);
end = strchr(ptmp, ' ');//查找到所需的数据
*end = '\0';
memcpy(start, ptmp, end-ptmp);//拷贝数据,进行返回
return start;
}
int main()
{
printf("MemFree:[%s]\n", parse_string("/proc/meminfo", "MemFree"));
return 0;
}