? Linux ?,sscanf
?????????????????
- ??,?????????????????????????????,?????????????:
[2021-09-01 12:34:56] INFO: User 'JohnDoe' logged in from IP 192.168.1.100
- ??
sscanf
?????????????,??????sscanf
????????????????:
#include#include int main() { char log[] = "[2021-09-01 12:34:56] INFO: User 'JohnDoe' logged in from IP 192.168.1.100"; char date[11], time[9], level[6], user[20], ip[20]; int result = sscanf(log, "[s %8s] %5s: User '[^']' logged in from IP s", date, time, level, user, ip); if (result == 5) { printf("Date: %s\n", date); printf("Time: %s\n", time); printf("Level: %s\n", level); printf("User: %s\n", user); printf("IP: %s\n", ip); } else { printf("Error parsing the log.\n"); } return 0; }
?????? sscanf
???????????:
[s %8s]
:???????,?????date
?time
????%5s
:??????(? INFO?WARNING ?),???level
????User '[^']'
:?????,???user
??????,??????????,[^']
????????????????logged in from IP s
:???????,???? IP ??,???ip
????
- ???????
sscanf
??????????????????,??????????????,????????????????
???,???????????????????????????,??????????? sscanf
???????????????,????????????????,????????????????