I'm a newbie to C Programming. I want to extract the values out of the input buffer string. I saw a couple of examples for sscanf and it works with space delimiter but it doesn't work with colons or comma.
I tried to use some regex in sscanf but still doesn't seems to work.
#include <stdio.h>
int main() {
char buffer[] = "T:192.164.7.1:22:user:pass:empty:test.txt";
char cmdChar;
char ipAddress[100];
int port;
char username[100];
char password[100];
char folder[100];
char fileName[100];
char fileExtension[100];
sscanf(buffer, "%1c:%[^:]%d:%s:%s:%s:%s.%s", cmdChar, ipAddress, &port, username, password, folder, fileName, fileExtension);
printf("%c \n\n", cmdChar);
}
Tried to print the first character cmdChar but it returns as NULL. Can someone point me out what am I doing wrong. Thanks!
&cmdChartosscanf. - tkausl"%1c"is rather useless since acharis just a single character anyway. The limit makes more sense for the strings to prevent buffer overflows. - Some programmer dude[^:]%dwill read until:and:left out in the string hence yourportwill have:ascii value . - KBlr