title: C program to merge files not finding EOF, stuck in infinite loop!
body: <p>I'm having some problems trying to run this program I am working on...The requirements say I was not allowed to use a sort function...I had do something myself....etc.</p>
  
  <p>Pretty much, the program compiles but hangs after executed...I'm guessing it's stuck in an infinite loop...but I can't seem to find it... :(</p>
  
  <p>This program reads to data files that will already be ordered least to greatest and merges them (ordered) into a third .txt file...</p>
  
  <p>The two files are Data1.txt and Data2.txt 
  contains:</p>
  
  <p>Data1.txt</p>
  
  <pre><code>2
  2
  2
  2
  </code></pre>
  
  <p>Data2.txt</p>
  
  <pre><code>1
  3
  5
  7
  9
  </code></pre>
  
  <p>combine.c</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  
  void sortData(FILE *fpData1, FILE *fpData2)
  {
  
   int n, m; 
   FILE *fpMerge;
  
  
   fpMerge = fopen("Merge.txt", "w+");
  
  
   fscanf(fpData2, "%i", &amp;n);
   fscanf(fpData1, "%i", &amp;m);
   while(n != EOF || m != EOF)
   {
  
    if(n == EOF)
    {
     fscanf(fpData1, "%i", &amp;m);
  
     while(m != EOF)
     {
  
      fprintf(fpMerge, "%i\n", m);
      fscanf(fpData1, "%i", &amp;m);
     }
    }
    if(m == EOF)
    {
     fscanf(fpData2, "%i", &amp;n);
  
     while(n != EOF)
     {
  
      fprintf(fpMerge, "%i\n", n);
      fscanf(fpData2, "%i", &amp;n);
     }
  
    }
  
    if(n &lt; m)
    {
     fprintf(fpMerge, "%i\n", n);
     fscanf(fpData2, "%i", &amp;n);
    }
    if(n &gt; m)
    {
     fprintf(fpMerge, "%i\n", m);
     fscanf(fpData1, "%i", &amp;m);
    }
    if(n == m)
    {
     fprintf(fpMerge, "%i\n", n); 
     fprintf(fpMerge, "%i\n", m);
     fscanf(fpData2, "%i", &amp;n);
     fscanf(fpData1, "%i", &amp;m);
  
    }
   }
  
   fclose(fpMerge);
  }
  
  int main (void)
  {
   FILE *fpData1;
   FILE *fpData2;
  
  
   fpData1 = fopen("Data1.txt", "r");
   if(fpData1 == NULL)
   {
    printf("There was an error opening the file...program terminating..\n");
    exit(1);
   }
  
   fpData2 = fopen("Data2.txt", "r");
   if(fpData2 == NULL)
   {
    printf("There was an error opening the file...program terminating..\n");
    exit(1);
   }
  
  
  
   sortData(fpData1, fpData2);
  
  
   fclose(fpData1);
   fclose(fpData2); 
  
   return 0;
  }
  </code></pre>