title: Converting Lower Case Characters in a Text File to Upper Case Letters and Vice Versa
body: <p>I have a text file that contains the following text, without a new line character...</p>
  
  <pre><code>Hello World
  </code></pre>
  
  <p>I would like to convert the lower case characters to upper case, and vice versa, so that the same text file would end up with the following text...</p>
  
  <pre><code>hELLOW wORLD
  </code></pre>
  
  <p>Unfortuntately, when I run my code, it goes into an endless loop.  When I step through the code, I see that fseek() goes back one byte for the first loop, as expected, but it goes back two bytes for the second and subsequent loops.  I don't understand why it goes back two bytes instead of one.  Why is this the case?  Can someone please help?</p>
  
  <p>Here's my code...</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;ctype.h&gt;
  
  int main()
  {
      FILE *fp;
      int ch;
      long offset;
  
      fp = fopen("c:\\users\\domenic\\desktop\\test.txt", "r+");
      if (fp == NULL)
      {
          printf("error:  unable to open file\n");
          exit(1);
      }
  
      offset = ftell(fp);
      while (1)
      {
          ch = fgetc(fp);
          if (ch == EOF)
              break;
          if (isupper(ch))
          {
              fseek(fp, offset, SEEK_SET);
              fputc(tolower(ch), fp);
          }
          else if (islower(ch))
          {
              fseek(fp, offset, SEEK_SET);
              fputc(toupper(ch), fp);
          }
          offset = ftell(fp);
      }
  
      fclose(fp);
  
      return 0;
  }
  </code></pre>