title: File Handling c++: using this pointer to read objects
body: <p>Is this correct?
  Here Update is member function of Product class.
  I am using this pointer(pointing to calling product object) to read objects stored in file over and over.</p>
  
  <pre><code>file.read((char *)this, sizeof(product));
  </code></pre>
  
  <p>Here if the class(see the update member function):</p>
  
  <pre><code>    class product
  {
      int product_num;
      string product_name;
      int quantity;
      float sell_p;
      float buy_p;
      int gst;
      float total;
      float profit;
  
    public:
      void calculate()
      {
          this-&gt;total = this-&gt;quantity * (this-&gt;sell_p);
          this-&gt;profit = this-&gt;total - (this-&gt;buy_p * this-&gt;quantity) * (1 + this-&gt;gst / 100);
      }
      product()
      {
          product_num = 0;
          product_name = "\0";
      }
      product(int p_num, string name, int quantity, float sell_p, float buy_p, int gst)
      {
          this-&gt;product_num = p_num;
          this-&gt;product_name = name;
          this-&gt;quantity = quantity;
          this-&gt;sell_p = sell_p;
          this-&gt;buy_p = buy_p;
          this-&gt;gst = gst;
          calculate();
      }
      void get_data()
      {
          cout &lt;&lt; "Enter Serial Number: ";
          cin &gt;&gt; this-&gt;product_num;
          cout &lt;&lt; "Enter Product Name(MAX LENGTH: 20): ";
          cin&gt;&gt;this-&gt;product_name;
          cout &lt;&lt; "Enter Quantity: ";
          cin &gt;&gt; this-&gt;quantity;
          cout &lt;&lt; "Enter Buying Price: ";
          cin &gt;&gt; this-&gt;buy_p;
          cout &lt;&lt; "Enter GST: ";
          cin &gt;&gt; this-&gt;gst;
          cout &lt;&lt; "Enter Selling Price: (Selling Price should be greater than " &lt;&lt; (this-&gt;buy_p + (this-&gt;gst / 100) * this-&gt;buy_p) &lt;&lt; " to turn profit)";
          cin &gt;&gt; this-&gt;sell_p;
          calculate();
      }
      void show_product()
      {
          cout &lt;&lt; "Serial: " &lt;&lt; this-&gt;product_num
               &lt;&lt; "\nName: " &lt;&lt; this-&gt;product_name
               &lt;&lt; "\nQuantity: " &lt;&lt; this-&gt;quantity
               &lt;&lt; "\nSelling Price: " &lt;&lt; this-&gt;sell_p
               &lt;&lt; "\nBuying Price: " &lt;&lt; this-&gt;buy_p
               &lt;&lt; "\nGST: " &lt;&lt; this-&gt;gst &lt;&lt; endl;
      }
      bool remove_product(int x)
      {
          if (x &gt; this-&gt;quantity)
              return false;
          this-&gt;quantity -= x;
          return true;
      }
      int getSerialNumber()
      {
          return this-&gt;product_num;
      }
      string getName()
      {
          return this-&gt;product_name;
      }
      int getQuantity()
      {
          return this-&gt;quantity;
      }
      int getGST()
      {
          return this-&gt;gst;
      }
      float getTotal()
      {
          return this-&gt;total;
      }
      float getProfit()
      {
          return this-&gt;profit;
      }
      float getSellPrice()
      {
          return this-&gt;sell_p;
      }
      float getBuyPrice()
      {
          return this-&gt;buy_p;
      }
      bool isValid()
      {
          if (product_num == 0 || product_name == "\0")
              return false;
          return true;
      }
      void save()
      {
          ofstream fout;
          fout.open("product.dat", ios::binary | ios::app);
          if (!fout)
          {
              cout &lt;&lt; "Failed To Open File!!\nExiting..";
              exit(0);
          }
          else
          {
              fout.write((char *)this, sizeof(product));
          }
          fout.close();
      }
      void update()
      {
          int s_num = this-&gt;product_num;
          fstream file;
          file.open("product.dat", ios::binary | ios::ate | ios::in | ios::out);
          if (!file)
          {
              cout &lt;&lt; "Failed to open file!!!\nExiting...." &lt;&lt; endl;
              exit(0);
          }
          else
          {
              file.seekg(0);
              file.read((char *)this, sizeof(product));
              while (!file.eof())
              {
                  if (s_num == this-&gt;product_num)
                  {
                      cout &lt;&lt; "Product Detail: ";
                      this-&gt;show_product();
                      cout &lt;&lt; "Enter the Values: " &lt;&lt; endl;
                      cout &lt;&lt; "Note: IF DONT WISH TO CHANGE KINDLY ENTER SAME VALUE.";
                      this-&gt;get_data();
                      file.seekp(-(sizeof(product)),ios::cur);
                      file.write((char *)this, sizeof(product));
                      break;
                  }
                  file.read((char *)this, sizeof(product));
              }
          }
          file.close();
      }
      void remove_it()
      {
          int s_num = this-&gt;product_num;
          ofstream fout;
          ifstream fin;
          fin.open("product.dat", ios::in | ios::binary);
          if (!fin)
          {
              cout &lt;&lt; "Failed to open file!!\nExiting...";
              exit(0);
          }
          else
          {
              fout.open("temp.dat", ios::binary | ios::out);
              fin.read((char *)this, sizeof(product));
              while (!fin.eof())
              {
                  if (!(s_num == this-&gt;product_num))
                  {
                      fout.write((char *)this, sizeof(product));
                  }
                  fin.read((char *)this, sizeof(product));
              }
              fin.close();
              fout.close();
              if(remove("product.dat") == 0)
                  cout&lt;&lt;"Sucessfully Deleted file\n";
              else
                  cout&lt;&lt;"File Removal Failed\n";
              if(rename("temp.dat", "product.dat") == 0)
                  cout&lt;&lt;"Successfully Renamed\n";
              else
                  cout&lt;&lt;"Failed to Rename\n";
          }
      }
  };
  </code></pre>