C语言基础 第九章 常用字符串操作函数
sscanf():可以将一个长的字符串按照需求分割成想要的格式
实例:
#include<stdio.h> #include<string.h> int main() { //1. 取指定长度的字符串 char str[100]; sscanf("12345","%4s",str); printf("%s\n",str);//1234 //2. 格式化时间 int year, month, day, hour, minute, second; sscanf("2013/02/13 14:55:34","%d/%d/%d %d:%d:%d",&year, &month, &day, &hour, &minute, &second); printf("time=%d-%d-%d %d:%d:%d\n", year, month, day, hour, minute, second);//time=2013-2-13 14:55:34 //3. %*d和%*s加了(*)表示跳过此数据不读入(也就是不把此数据读入参数中) sscanf("1234abcd","%*d%s",str);//此处表示跳过 %d 的数据,也就是跳过 :1234 printf("%s\n",str);// abcd //4. 取到指定字符为止的字符串。如例子所示,遇到‘+’为止的字符串 sscanf("1234+abc","%[^+]",str); printf("%s\n",str);// 1234 //5. 取到指定字符集为止的字符串。如遇到小写字母为止的字符串。 sscanf("1234+abc1234","%[^a-z]",str); printf("%s\n",str);// 1234+ //6. 取仅包含指定字符集的字符串。(取仅包含数字和小写字母的字符串,是取得连续的字符串)。 sscanf("123456abcdefBFRGTY7890","%[1-9a-z]",str); printf("%s\n",str);// 123456abcdef return 0; }
实际应用:
读取 pclist_info 文件内容 ,90:2b:34:67:2b:43 [192.168.10.111] [] [] [0 1900/01/00 00:00:00] [Wire] [STATIC] [Online] [] [] [Used]
此函数遇到一个问题,当遇到, [] 第一个符号时,里面是空的,后面的就无法获取了
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<time.h> typedef struct { char hostClass[8]; char hostMAC[20]; char hostDHCPName[70]; char hostDevName[70]; char hostIP[20]; char hostConnectInterface[8]; char hostOnlineTime[64]; char hostStotageAccessStatus[8]; }CMCCLanHostInfo; int main(void) { FILE *fp = NULL; char line[512] = {0}; int i=0; struct tm timeinfo={0}; CMCCLanHostInfo tmp_lhi={0}; char if_mac[20]={0}; char contype[20]={0}; char conssid[20]={0}; char ip_addressing[20]={0}; char time[128] =""; char *p = NULL; char dest[] = "[]"; if ((fp = fopen("pclist_info", "r")) == NULL) { printf("%s", "Cannot open pclist_info file!\n"); return 0; } if(fgets(line, sizeof(line)-1 , fp) != NULL ) { line[strlen(line)-1] = ‘\0‘; // 90:2b:34:67:2b:43 [192.168.10.111] [0] [1] [0 1900/01/00 00:00:00] [Wire] [STATIC] [Online] sscanf(line, "%[^]%*[^[][%[^]] %*[^[][%[^]] %*[^[][%[^]] %*[^[][%[^]] %*[^[][%[^]] %*[^[][%[^]] %*[^[][%[^]] %*[^[][%[^]] %*[^[][%[^]] %*[^[][%[^]]", tmp_lhi.hostMAC, tmp_lhi.hostIP, tmp_lhi.hostDevName, tmp_lhi.hostDHCPName, time, contype, ip_addressing, tmp_lhi.hostClass, tmp_lhi.hostConnectInterface, conssid, tmp_lhi.hostStotageAccessStatus); printf("hostMAC = %s \n",tmp_lhi.hostMAC); printf("hostIP = %s \n",tmp_lhi.hostIP); printf("hostDevName = %s \n",tmp_lhi.hostDevName); printf("hostDHCPName = %s \n",tmp_lhi.hostDHCPName); printf("time = %s \n",time); printf("contype = %s \n",contype); printf("ip_addressing = %s \n",ip_addressing); printf("hostClass = %s \n",tmp_lhi.hostClass); printf("hostConnectInterface = %s \n",tmp_lhi.hostConnectInterface); printf("conssid = %s \n",conssid); printf("hostStotageAccessStatus = %s \n",tmp_lhi.hostStotageAccessStatus); } fclose(fp); return 0; }
相关推荐
lynjay 2020-06-14
88384957 2020-06-12
zuihaobushi 2020-04-30
xiaohouye 2020-04-20
菇星獨行 2020-04-20
qscool 2020-04-16
Joymine 2020-03-04
CosEmon 2020-03-01
GoatSucker 2020-02-15
Joymine 2020-02-11
twater000 2020-02-01
AaronPlay 2020-06-13
徐建岗网络管理 2020-07-28
ILVNMM 2020-10-26
PinkBean 2020-08-19
Seandba 2020-08-16
herohope 2020-06-10
adwen00 2020-06-09