0
Petroleum test = Set_Petroleum_values();
    ofstream fuel("Fuel.txt");
    {
        fuel << test << " ";
        fuel.close();
    }

the set values function takes the users input and calls a default constructor for the test object. My aim here is to somehow save the info that is being stored into this object however i am not sure how would i overload the "<<" operator when it comes to file handling. and yes the aim is to store the object so that the info is maintained once different operations and functions are proceeded etc.

  • That's just a usual overload of the streaming operator, the fact that the stream outputs into a file does not matter. – Quentin May 02 '18 at 10:09

1 Answers1

0

I think you should overload << operator in Petroleum class like this :

#include <iostream>
#include <fstream>

class Petroleum
{
public:
    Petroleum(int v1,int v2) : val1(v1), val2(v2) {}
    friend std::ostream& operator<<(std::ostream& os, const Petroleum& pet);
private:
    int val1;
    int val2;
};

std::ostream& operator<<(std::ostream& os, const Petroleum& pet)
{
    return os << pet.val1 << " " << pet.val2 << std::endl;
}

int main()
{
    Petroleum pet(2, 3);

    std::ofstream fuel("Fuel.txt");
    fuel << pet;
    fuel.close();
}
mystic_coder
  • 462
  • 2
  • 10