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!

  1. use male[i] rather &male[i].

  2. limit string input 99 (1 less size of buffer space)

  3. check return value of sscanf()

  4. 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

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

c# - Unity IoC Lifetime per HttpRequest for UserStore -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -