sscanf (): a long string may be divided into a desired format on demand
Example:
#include<stdio.h> #include<string.h> int main() { // string 1. Take specified length char STR [ 100 ]; sscanf("12345","%4s",str); printf("%s\n",str);//1234 // 2. Formatting time int year, month The, 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 and% * s * Added (*) means skip the data is not read (i.e. the data is not read into the parameter) sscanf ( " 1234ABCD " , " % S% D * " , STR); // herein denotes% d skip data, that is skipped: 1234 the printf ( " % S \ n- " , STR); // ABCD // 4 to take up the specified character string. As the example shown, face '+' until the string sscanf ( " 1234 + ABC " , " % [^ +] " , STR); printf("%s\n",str);// 1234 // 5. Take the string until the specified character set. In case of the string until the lowercase letters. sscanf ( " 1234 + ABC1234 " , " % [^ AZ] " , STR); printf("%s\n",str);// 1234+ // 6. Take strings containing only the specified character set. (Containing only take lower case letters and numbers string of continuous string is acquired). sscanf ( " 123456abcdefBFRGTY7890 " , " % [1-9a-Z] " , STR); printf("%s\n",str);// 123456abcdef return 0; }
Practical application:
Pclist_info read content file, 90: 2b: 34: 67: 2b: 43 [192.168.10.111] [] [] [0 1900/01/00 00:00:00] [Wire] [STATIC] [Online] [] [] [Used]
This function encountered a problem when encountered, [] when the first symbol, which is empty, you can not get back up
#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;
}