2

我收到一条syslog消息

<14>2012-04-19T03:54:18+08:00.527800 server3000 status: 00|00|The average CPU, memory, disk usage and total network flow are 5.0%, 5.0%, 5.0% and 5 bytes respectively for the last hour.\n

如何2012-04-19,03:54:18+08:00.527800,server3000,status,00,00 and 5.0通过函数获取单词sscanf

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

int main(void) {
int t1,t2 = 0;
char programname[20], deviceip[16], date[11], time[9];
char logmsg[30];
const char * s = "<14>2012-04-19T03:54:18.527800+08:00 server3000 status: 00|00|The average CPU, memory, disk usage and total network flow are 5.0%, 5.0%, 5.0% and 5 bytes respectively for the last hour";
sscanf(s, "<%*d>%[^T]T%[^.].%*[^ ] %[^ ] %[^:]: %d|%d|%[^\n]", date, time,deviceip, programname, &t1, &t2, logmsg);
return 0;
}
4

1 回答 1

0

您可能希望保存结果sscanf并检查其值:

int rc = sscanf(...);
if(rc < 7)
    // failed to match or extract

sscanf在您的代码中返回 7,这意味着您的格式字符串匹配并提取了所有 7 个参数。

于 2012-04-19T08:34:05.237 回答