title: Flush function in cpp
body: <p>I have looked up the definition of the function flush in C++, and I got some really satisfactory answers, but I recently came across the following code, and I can't seem to understand if the use of flush is making much of a difference here, it seems that the code is giving a valid output even without the use of flush. Please help out!</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  class person {
  public:
      int ph_no;
      char name[50];
      void accept() {
          cout&lt;&lt;"\nEnter name";
          cin&gt;&gt;name;
          cout&lt;&lt;"\nenter ph_no";
          cin&gt;&gt;ph_no;
      }
      void display() {
          cout&lt;&lt;"name:"&lt;&lt;name&lt;&lt;"\n";
          cout&lt;&lt;"phone_no:"&lt;&lt;ph_no&lt;&lt;"\n" ;
      }
  };
  
  int main() {
   // a few other functions to create file and read file &amp;c &amp;c.
     person p;
     int pno,pos,choice,offset,i;
     fstream fp;
     char name[20];
  
     cout&lt;&lt;"\n enter name";
     cin&gt;&gt;name;
     fp.open("d:\\test.dat",ios::out|ios::in|ios::ate|ios::binary);
     fp.seekg(0,ios::beg);
     pos=-1;
     i=0;
     while(fp.read((char *)&amp;p,sizeof(p))) {
        if((strcmp(name,p.name))==0) {
                             pos=i;
                             break;
        }
        i++;
     }
     offset=pos*sizeof(p);
     fp.seekp(offset);
     cout&lt;&lt;"\ncurrent phno:"&lt;&lt;p.ph_no;
     cout&lt;&lt;"\nenter new phone no";
     cin&gt;&gt;pno;
     p.ph_no=pno;
     fp.write((char *)&amp;p,sizeof(p))&lt;&lt;flush;
     cout&lt;&lt;"\nrecord updated\n";
     fp.seekg(0);
     while(fp.read((char *)&amp;p,sizeof(p))) {
                    p.display();
     }
     fp.close();
     return 0;
   }
  </code></pre>