scanf - sscanf() in C to skip over an integer and read string -
i have file contains data in form:
1 jake 234 ruby 98.
i want use sscanf
read strings arrays, tried this:
male[i] = malloc(100); female[i] = malloc(100); sscanf(str, "%*d%s%*d%s%*d", &male[i], &female[i]);
the problem when i = 0
, function skips first string along first integer. when try print &male[0]
, blank space.
i have initialised i
0
. please point out might going wrong?
thanks lot!
use
male[i]
rather&male[i]
.limit string input 99 (1 less size of buffer space)
check return value of
sscanf()
the last
"%*d"
not anything.
-
char *male[n]; // assumed sample declaration char *female[n]; male[i] = malloc(100); female[i] = malloc(100); int cnt = sscanf(str, "%*d%99s%*d%99s", male[i], female[i]); if (cnt == 2) success();
if want insure data parsed , no non-white-space @ end....
int n = 0; int cnt = sscanf(str, "%*d%99s%*d%99s%*d %n", male[i], female[i], &n); if (cnt == 2 && str[n] == '\0') success();
Comments
Post a Comment