I'm trying to scanf words and numbers from a string looks like: "hello, world, I, 287876, 6.0" <-- this string is stored in a char
array (string)
What I need to do is to split things up and assign them to different variables so it would be like
char a = "hello"
char b = "world"
char c = "I"
unsigned long d = 287876
float e = 6.0
I know that regular scanf stops reading from stdin when it reaches a white space. So I've been thinking that there might be a way to make sscanf stop reading when it reaches a "," (comma)
I've been exploring the library to find a format for sscanf to read only alphabet and numbers. I couldn't find such a thing, maybe I should look once more.
Any help? Thanks in advance :)
If the order of your variables in the string is fixe, I mean It's always:
string, string, string, int, float
the use the following format specifier in sscanf()
:
int len = strlen(str);
char a[len];
char b[len];
char c[len];
unsigned long d;
float e;
sscanf(" %[^,] , %[^,] , %[^,] , %lu , %lf", a, b, c, &d, &e);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With