sscanf学习

该代码段展示了如何使用C语言解析`+CCLK`时间字符串,并将其转换为结构体`timebase`。程序首先通过`strcpy`复制时间字符串,然后使用`sscanf`解析日期和时间信息,最后将这些信息打印出来。这段代码适用于需要处理时间数据的嵌入式或物联网应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
	unsigned char DeviceID[10];//IMEI
	unsigned char date[50];
	unsigned char time[50];
	int tempature;
	int humidity;
    int voltage;
    int current;
    float longitude;
    float latigude;
    int airDistance;
    char errorCode;
}Info_send;
typedef struct
{
    int tm_sec;
    int tm_min;
    int tm_hour;
    int tm_mday;
    int tm_mon;
    int tm_year;
}timebase;
int time;
Info_send message_Send;
char timestring[30];
char timerecord[20];
int flag;
int main()
{
    timebase tm;
    strcpy(timestring,"+CCLK: \"21/07/23,14:56:30+32\"");
    printf("%s\n",timestring);
    sscanf(timestring,"+CCLK: \"%d/%d/%d,%d:%d:%d\"",&tm.tm_year,&tm.tm_mon,&tm.tm_mday,&tm.tm_hour,&tm.tm_min,&tm.tm_sec);
    printf("%d-%d-%d %d:%d:%d\n",tm.tm_year,tm.tm_mon,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
     //从缓冲区中解析json结构

    //printf("%c\n",timestring[8]);

    //结构体数据赋予字符串
    //strcpy(message_Send.DeviceID,"YRA001");

    //printf("%s\n",message_Send.DeviceID);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值