title: Cutting a string into pieces
body: <p>How can I extract pieces from this string?</p>
  
  <p>I have a file that contains:</p>
  
  <pre><code>0065445 APPLE$456
  089464 MANGO$489
  0012389 GUAVA$744
  </code></pre>
  
  <p>What I want to do is input the file line by line, then cut the string into some pieces.</p>
  
  <ul>
  <li>0065455 Will go in a struct <code>a[0].num</code>  </li>
  <li>APPLE will go in struct <code>a[0].name</code></li>
  <li>456 will go in struct <code>a[0].dollar</code></li>
  </ul>
  
  <p>And similarly for other lines.</p>
  
  <p>Everything is working fine, but it's not successfully getting the dollar part into its variable.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>#include&lt;cstdlib&gt;
  #include&lt;iostream&gt;
  
  using namespace std ;
  
  int main(){
  
  FILE *fp;
  
  fp = fopen("input.txt","r");
  
  char str[80] ;
  
  struct abc{
  int num;
  char name[20];
  int dollar;
  };
  
  int i = 0;
  
  while(fgets(str,79,fp)!=NULL){
  
  struct abc a[i] ;
  
  sscanf(str,"%d %[^$]s$%d\n",&amp;a[i].num,a[i].name,&amp;a[i].dollar);
  
  cout &lt;&lt;i+1 &lt;&lt;") Number : "&lt;&lt;a[i].num&lt;&lt;" Name :  "&lt;&lt; a[i].name &lt;&lt;" Dollar : "&lt;&lt; a[i].dollar &lt;&lt; endl ;
  i++;
  
  }
  
  return 0 ;
  }
  /* These didn't work too.
  sscanf(str,"%d %[^$]s %d\n",&amp;a[i].num,a[i].name,&amp;a[i].dollar);
  sscanf(str,"%d %[^$]s%d\n",&amp;a[i].num,a[i].name,&amp;a[i].dollar);
  sscanf(str,"%d %s$%d\n",&amp;a[i].num,a[i].name,&amp;a[i].dollar);
  
  */
  </code></pre>
  
  <p>There's 1 more problem: the first part of string is an int that starts with 0, but the zero is not being accepted in the int. How to do it?</p>
  
  <p>This is working as I want now but still after parasing the string into an int I am not getting the zeroes:</p>
  
  <pre><code>#include&lt;cstdlib&gt;
  #include&lt;iostream&gt;
  #include&lt;cstring&gt;
  
  using namespace std ;
  
  int main(){
  
      FILE *fp;
  
      fp = fopen("input.txt","r");
  
      char str[80] ;
      char temp[80] ;
      struct abc{
          int num;
          char name[20];
          int dollar;
      };
  
      int i = 0;
      int j = 0 ;
  
      while(fgets(str,79,fp)!=NULL){
          i = 0;
          j = 0 ;
          struct abc a[i] ;
  
          char* ptr = 0; // this is used as a helper variable to strtok
  
          ptr = strtok(str, " $\n"); // we specify the delimiters here
  
          while (ptr != NULL) 
          {
              if (j == 0){
                  strcpy(temp, ptr);
                  a[i].num = atoi(temp);
              }
              if (j == 1)
                  strcpy(a[i].name, ptr);
  
              if (j == 2){
                  strcpy(temp, ptr);
                  a[i].dollar = atoi(temp);
              }
  
              ptr = strtok(NULL, " $\n");
              j++;
          }
  
          cout &lt;&lt;i+1 &lt;&lt;") Number : "&lt;&lt;a[i].num&lt;&lt;" Name :  "&lt;&lt; a[i].name &lt;&lt;" Dollar : "&lt;&lt; a[i].dollar &lt;&lt; endl ;
          i++;
      }
  
      return 0 ;
  }
  
  /* These didn't work either.
  sscanf(str,"%d %[^$]s %d\n",&amp;a[i].num,a[i].name,&amp;a[i].dollar);
  sscanf(str,"%d %[^$]s%d\n",&amp;a[i].num,a[i].name,&amp;a[i].dollar);
  sscanf(str,"%d %s$%d\n",&amp;a[i].num,a[i].name,&amp;a[i].dollar);
  
  */
  </code></pre>