title: Reading file line by line and using strtok() in C
body: <p>I'm trying to read a configuration file line by line and then tokenize and store the result into separate variables. My configuration file looks like the following</p>
  
  <pre><code>stage 1
  num_nodes 2
  nonce 234567
  </code></pre>
  
  <p>I need to tokenize each value in the line separately, so for instance in the first line "stage" used to check if I have read the stage value from the configuration file and then save its value in a variable. My tokenizing seems to be working correct. However, when I try to manipulate my variables after tokenizing it gives me a segmentation fault. At most I can just successfully manipulate one of the variables, i.e. either stage or num_nodes or nonce but not a combination of them. Even if try to do something like</p>
  
  <pre><code>stage = stage + 1;
  num_nodes = num_nodes + 1;
  </code></pre>
  
  <p>this gives a segmentation fault, however, if I just make a change to one variable, such as:</p>
  
  <pre><code>num_nodes = num_nodes + 1;
  </code></pre>
  
  <p>then it works fine. I'm pasting the code below, kindly tell me what I'm missing here.</p>
  
  <pre><code>main(int argc, char *argv[]){
    int nonce;
    int num_nodes;
    int stage; 
    char filename[256];   
    char *token1, *token2, *str;  
    FILE* fp;
    char bufr[MAXLINE];  
  
    printf("Please enter config file name\n");
    scanf("%s",filename);
    printf("You entered %s\n", filename);
  
    if((fp = fopen(filename, "r")) != NULL){
  
          while(fgets(bufr, MAXLINE, fp) != NULL){
              if(bufr[0] == '#') // to skip comments
                  continue;
  
              printf("This is bufr: %s",  bufr);
              str = bufr;
  
                for(str;  ;str = NULL){
                  token1 = strtok(str, " ");
  
                  if(strcmp(token2, "num_nodes") == 0){
                      num_nodes = atoi(token1); 
                      printf("num_nodes = %d\n", num_nodes);
                  }
  
                  if(strcmp(token2, "nonce") == 0){
                      nonce = atoi(token1);
                      printf("nonce = %d\n", nonce);
                  }       
  
                  if(strcmp(token2, "stage") == 0){
                      stage = atoi(token1);
                      printf("stage = %d\n", stage);
                  }                   
  
                  token2 = token1; // making a copy of pointer
  
                  if(str == NULL){
                      break;
                  }
            }//end of for loop
  
          }//end of while loop
          fclose(fp); //close the file handle
      }
      else{
          printf("failed, file not found!\n");
      }
  
  /*      This is where the segmentation fault kicks in, try to uncomment two lines and it will give a segmentation fault, if uncomment just one, then it works fine.
      nonce = nonce + 2;  
      num_nodes = num_nodes + 1;
      printf("stage = %d\n", stage);
  */
  }
  </code></pre>