title: C File I/O bug in my code
body: <p>I attempted writing a thesaurus program which reads a thesaurus file, for example: 
  drink:beverage
  clever:smart,witty
  and a .txt document, changing up the words it finds from the thesaurus and creating a new document with the modified text. However there appears to be a bug, I have narrowed it down to the while loop in getReplacement(), by checking a print operation before and after. I would really appreciate someone finding why it won't work.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;ctype.h&gt;
  #include &lt;string.h&gt;
  #include &lt;iostream&gt;
  
  
  char* getReplacement(char* original, FILE* file);
  
  int main(int argc, char* argv[])
  {
  
      using namespace std;
  
      FILE* thes = fopen(argv[1], "r");
      FILE* text = fopen(argv[2], "r+");
      FILE* nText = fopen("temp.txt", "w");
      if(thes == NULL || text == NULL || nText == NULL)
          return 1;
      char word[20] = {};
      char c;
      int bytesW=0;
      while((c = fgetc(text)) != EOF)
      {
          fputc(c, nText);
          bytesW++;
          if(isalpha(c))
          {
              int len = strlen(word);
              word[len] = c;
              word[len + 1] = '\0';
          }
  
          else
          {
              if(word == "")
                  continue;
              cout &lt;&lt; 7&lt;&lt;endl;
              char* replacement = getReplacement(word, thes);
              if(replacement == NULL)
                  continue;
              fseek(nText,bytesW-1-strlen(word),SEEK_SET);
              for(int i=0;i&lt;strlen(replacement);i++)
                  fputc(replacement[i],nText);
              int diff = strlen(word) - strlen(replacement);
              while(diff-- &gt;0)
                  fputc(' ', nText);
              bytesW = bytesW-1-strlen(word)+strlen(replacement);
              fseek(nText, bytesW, SEEK_SET);
          }
  
      }
      fclose(thes);
      fclose(text);
      fclose(nText);
  
  
      return 0;
  }
  
  char* getReplacement(char* const original, FILE* file)
  {
      using namespace std;
      char* line="";
      const short len = strlen(original);
      int numOfOptions=1;
      int toSkip=0; // number of commas to skip over
      outer: while(fgets(line,1000,file) != NULL)
      {
          for(int i=0;i&lt;len;i++)
              if(line[i] != original[i])
              {
                  goto outer;
              }
          if(line[len] != ':') 
              goto outer;
          for(int i=0;i&lt;len;i++)
              line++;
          for(int i=0;i&lt;strlen(line);i++)
              if(line[i] == ',')
                  numOfOptions++;
          toSkip = rand()%numOfOptions;
          while(toSkip &gt;0)
          {
              if(line[0] == ',')
                  toSkip--;
              line++;
          }
          return line;
      }
      return NULL;
  
  }
  </code></pre>