c - Which format should I use: scanf \string,string,int,int/? -


i have data in following format \a,b,c,d/ a,b strings of letters , numbers; c, d integers.

i tried using format \%s,%s,%d,%d/ format scan it, causes a,b,c,d/ scanf'ed first string instead of a.

question:

is there type in format in order achieve desired result?

you can use following format string use commas delimiters :

"\\%[^,],%[^,],%d,%d/" 

the idea tell scanf read isn't comma each string, read delimiting comma , continue.

here (bad , unsafe!) example:

char a[100], b[100]; int c=0, d=0;  scanf("\\%[^','],%[^','],%d,%d/", a, b, &c, &d); printf("%s, %s, %d, %d\n", a, b, c, d); 

in real code, you'll want write safer. can example use fgets read full line of input reuse same format string sscanf parse it.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -