title: Issues with flushing cout after getting file data
body: <p>I am trying to write an implementation of rc4. I am reading in plaintext from a file using an ifstream. I noticed that it wasn't outputting at the end of the file, so I tried the various ways of explicitly clearing the buffers. No matter which way (using an endl, appending \n, calling cout.flush()) I try to flush the buffer, I get a segfault. As a sanity check, I replaced my code with an example from the web, which I also tested separately. It works if I put it in its own file and compile it (e.g., it prints out the contents of the file, doesn't segfault, and doesn't require any calls to flush() or endl to do so), but not in my code.</p>
  
  <p>Here is the offending bit of code (which works fine outside of my code; its copied pretty much directly from cplusplus.com)</p>
  
  <pre><code> ifstream is;
   is.open("plain");
   char c;
   while (is.good())     // loop while extraction from file is possible
   {
       c = is.get();       // get character from file
       if (is.good())
           cout &lt;&lt; c;
  //       cout.flush();
   }
   is.close();           // close file*/
  </code></pre>
  
  <p>Here is the full code: (warning, lots of commented out code)</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string.h&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  static char s[256], k[256];
  //static char *i, *j;
  void swap(int m, int n, char t[256]){
          char tmp = t[m];
          t[m] = t[n];
          t[n] = tmp;
  }
  
  char getByte(){
          static char i(0), j(0);
          i = (i+1)%256;
          j = (j + s[i])%256;
          swap(i, j, s);
          return s[(s[i]+s[j]) % 256];
  }
  
  int main(int argc, char ** argv){
          /*string key = argv[1];*/
          if(argc &lt; 4){
                  cout &lt;&lt; "Usage: \n rc4 keyfile plaintextfile outputfile" &lt;&lt; endl;
                  return -1;
          }
          string key;
          ifstream keyfile (argv[1]);
          keyfile &gt;&gt; k;
          cout &lt;&lt; "Key = " &lt;&lt; k &lt;&lt; endl;
          keyfile.close();
          /*ifstream plaintextf;
          plaintextf.open(argv[2]);*/
  
          ofstream ciphertextf (argv[3]);
  
          for(int q = 0; q &lt; 256; q++){
                  s[q] = q;
          }
          int i, j;
          for(int m = 0; m &lt; 256; m++){
                  j = (j + s[m] + k[m % sizeof(k)])%256;
                  swap(m, j, s);
          }
  //      vector&lt;char&gt; bytes(plaintext.begin(), plaintext.end());
  //      bytes.push_back('\0');
  //      vector&lt;char&gt;::iterator it = bytes.begin();
  /*      char pt;
          while(plaintextf.good()){
                  pt = plaintextf.get();
                  if(plaintextf.good()){
                          cout &lt;&lt; pt;
  
                        ciphertextf &lt;&lt;(char) (pt ^ getByte());
                  }
  
          } */
          ifstream is;
          is.open("plain");
          char c;
           while (is.good())     // loop while extraction from file is possible
           {
              c = is.get();       // get character from file
              if (is.good())
                cout &lt;&lt; c;
  //              cout.flush();
           }
    is.close();           // close file*/
  
  /*//    plaintextf.close();
          ciphertextf.close();
          keyfile.close();
          */
          return 0;
  }
  </code></pre>