title: Outfile causes crashes with a memory violation
body: <p>I am writing a program to convert numbers from decimal to binary. I already have the algorithm correct for it and the program works fine while using cout. However, as soon as I use outfile in my loop the program crashes with error code (0xC0000005).
  Here is my source code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  #include &lt;fstream&gt;
  #include &lt;cmath&gt;
  using namespace std;
  
  int main()
  {
       int num, remainder_count;
       ifstream infile; //define new input file stream
       ofstream outfile; //define new output file stream
  
       infile.open("C:\\Users\\Arctic-Gaming\\CLionProjects\\working\\Source\\Binary Conversion (Loop w File)\\Binary Input.txt"); //connect the stream to an actual file
       if (!infile)
       {
           cout &lt;&lt; "Cannot open input file! Program aborted" &lt;&lt; endl;
           return 1;
       }
  
       outfile.open("C:\\Users\\Arctic-Gaming\\CLionProjects\\working\\Source\\Binary Conversion (Loop w File)\\Decimal Output.txt"); //connect the stream to an actual file
  
       do
       {
           int remainder [15] = {0};
           remainder_count = 15;
  
           infile &gt;&gt; num;
           outfile &lt;&lt; "\n" &lt;&lt; num &lt;&lt; endl;
           if (num &gt; 0 &amp;&amp; num &lt;= 65535)
           {
               while (num &gt; 0)
               {
                   remainder[remainder_count] = num % 2;
                   num /= 2;
                   remainder_count--;
               }
  
               remainder_count = 0;
               while (remainder_count &lt; 16)
               {
                   if (remainder_count % 4 == 0)
                   {
                       outfile &lt;&lt; " ";
                   }
                   outfile &lt;&lt; remainder[remainder_count];
                   remainder_count++;
               }
           }   
           else if (num == 0)
               outfile &lt;&lt; "0000 0000 0000 0000" &lt;&lt; endl;
  
           else
               cout &lt;&lt; "Error! Invalid Input." &lt;&lt; endl;
       }   
       while (!infile.eof());
  }   
  </code></pre>