title: Writing data to a text file column wise in C++
body: <p>I am a beginner in C++. I have input the values of the variables(belonging to a class) through an array of objects. Now how do I write them to the text file column wise as follows?? Thanks.........</p>
  
  <pre><code>SLNO    NAME      ADDRESS        PHONE NO     TYPE      
     1.   ABC       xyzagsgshsh    27438927     Mobile
     2.   QWE       qwhjbbdh       78982338     Landline
  </code></pre>
  
  <p>This is my code for storing the data. How do I make it into a text file with contents as below?</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;string&gt;
  #include&lt;fstream&gt;
  using namespace std;
  
  class emp
  {
      string name,address,phone,type;
      public:
      void getdata();
  }obj[5];
  
  void emp::getdata()
  {
      cout&lt;&lt;"\nEnter the details:";
      cout&lt;&lt;"\nName: ";cin&gt;&gt;name;
      cout&lt;&lt;"Address:";
      cin&gt;&gt;address;
      cout&lt;&lt;"Phone number: "; cin&gt;&gt;phone;
      cout&lt;&lt;"\nType of phone? (Landline/Mobile) :";
      cin&gt;&gt;type;
  }
  
  int main()
  {
      ofstream ptr;
      ptr.open("Phone.dat",ios::out);
      cout&lt;&lt;"\nEnter the no.of.records: ";
      int n,i;
      cin&gt;&gt;n;
      for(i=0;i&lt;n;i++)
      {
          obj[i].getdata();
          ptr.write((char*)&amp;obj[i],sizeof(obj[i]));
      }
      return 0;
  }
  </code></pre>