scan string with unknown number of elements to an array of int in C -
scan string with unknown number of elements to an array of int in C -
i'm working in programme in c , have char string of numbers this:
5 13 12 7 3 0    i want scan , set each of these integers in array of int. how do that? way utilize sscanf this?
i tried next code no success:
 fgets(datatemp, n*3, stdin);   k = 0; garb = 'c';     while(garb != '\0'){       garb = strtok(datatemp, " ");       array[k] = garb;       k++;     }    note: have utilize in function same many info in 'datatemp' string have unknown number of integers (and integers).
thanks help.
   // adapted strtok(3) example.  #include<stdio.h> #include<stdlib.h> #include<string.h>  int main(int argc, char *argv[]) {   char *str1, *str2, *token, *subtoken;   char *saveptr1,*saveptr2;   int j;   char datatemp[ 120 ];   char delim = ' ';    fgets(datatemp, 119, stdin);    /*    * strtok has called pointer input    * first time pointer string    * subsequently    */    for(j=1, str1 = datatemp;; j++, str1 = null)   {     token = strtok_r(str1, &delim, &saveptr1);      if(token==null)       break;      (str2 = token; ; str2 = null)     {       subtoken = strtok_r(str2, &delim, &saveptr2);       if (subtoken == null)         break;       printf(" --> %s\n", subtoken);     }   }    exit(0);    
}
 c arrays string strtok sscanf 
 
Comments
Post a Comment