title: while loop terminating before it is supposed to C
body: <p>This program is supposed to copy an existing txt file to a new txt code file. However is isn't working right. For some reason it always stops after the third iteration. 
    Suggestions?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  int main(void) {
      char fileNameIn[100];
      char fileNameOut[100];
      FILE *ptrIn = NULL; //____ File Pointers
      FILE *ptrOut = NULL;
      char str[1000]; //this is used at fgets and it obtains sentences
      /*_________________Counter variables)*/
      char *token;
      int ctr = 0;
      int ndel = -1;
      char wordA[10];
      char sentence[101];
  
      char del[10] = " !-,.";
      ;
      int temp = 0;
      printf("Enter the input filename: \n");
      //  gets(fileNameIn);
      scanf("%s", fileNameIn);
      //printf("You entered: %s\n",fileNameIn);
      printf("Enter the output filename: \n");
      scanf("%s", fileNameOut);
  
      ptrIn = fopen(fileNameIn, "r"); // r is to read
      ptrOut = fopen(fileNameOut, "w"); //w is to write on file
      if (ptrIn == NULL || ptrOut == NULL) {
          printf("Unable to open file\n");
          exit(1);
      }
  
      //while(fgets (str,sizeof(str), ptrIn) )
      while (fgets(str, sizeof(str), ptrIn)) { // while we are not at the end of the file
          puts(str);
  //      if(temp==0)
  //      {
          token = strtok(str, del);
          temp = -1;
  
          printf(
                  "Enter position of word to delete (Start counting at 0). Enter -1 to skip deletion:\n");
          scanf("%d", &amp;ndel);
          printf("You selected: %d\n", ndel);
  
          while (token != NULL)   // while loop inside a sentence
          {
              if (ctr != ndel) {
                  strcpy(wordA, token);
  
              }
              token = strtok(NULL, del);
              if (ctr != ndel) {
                  strcat(sentence, wordA);
                  strcat(sentence, " ");
                  printf("halfway?");
              }
              ctr++;
          }   // endof sentence loop
          fprintf(ptrOut, "%s", sentence);
          printf("the sentence is now:\n%s", sentence);
          printf("___________________________________________");
          printf("\n");
          strcpy(sentence, "");
          ctr = 0;
          ndel = -1;
      }   //end of while loop eof
  
      printf("Finish the main: ");
      fflush(ptrOut);
      fclose(ptrIn);
      fclose(ptrOut);
  
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>This is an example of the existing file:</p>
  
  <p>test.txt:
  </p>
  
  <pre class="lang-none prettyprint-override"><code>hello my name is john.
  this is a test.
  after the third line the while
  loop stops
  this does the get copied
  </code></pre>