rakesh
rakesh

Reputation: 1

operator overloading

How can I overload << operator (cout) so that it can be written to a file. Whenever << occurs it should be appended to the file i.e. instead of displaying on the screen it should be written to the file.

can i get a code for this...i am new to c++ and code written by me is not working..

Upvotes: 0

Views: 1484

Answers (4)

johnsyweb
johnsyweb

Reputation: 141770

If you just want to send text to a file rather than the console, then std::ofstream is your friend. Like cout, your instance of ofstream shares the behaviour of ostream which already has a suitable << operator for strings and can be used exactly as cout. Here's an example:

#include <fstream>

int main()
{
    std::ofstream fout("somefile.txt");

    fout << "Some text" << std::endl;

    return EXIT_SUCCESS;
}

Compile and run and you'll find that you have a new file in the current working directory:

% cat somefile.txt 
Some text

If you need to stream an object that doesn't have operator << overloaded for std::ostream, then you you will need to overload the operator, like this:

std::ostream& operator <<(std::ostream& os, const SomeClass& instance)
{
    instance.print(os);
    return os;
}

You'll need void SomeClass::print(std::ostream& os) defined to send its private data to os (this saves you making the << a friend of your class).


Were you talking purely about cout, then this isn't "operator overloading", this is stream redirection. But, yes, it is possible (too) by setting the associated stream buffer using std::basic_ios::rdbuf().

Here is my solution from "Redirecting in C++":

#include <iostream>
#include <fstream>

class scoped_cout_redirector
{
public:
    scoped_cout_redirector(const std::string& filename)
        :backup_(std::cout.rdbuf())
        ,filestr_(filename.c_str())
        ,sbuf_(filestr_.rdbuf())
    {
        std::cout.rdbuf(sbuf_);
    }

    ~scoped_cout_redirector()
    {
        std::cout.rdbuf(backup_);
    }

private:
    scoped_cout_redirector();
    scoped_cout_redirector(const scoped_cout_redirector& copy);
    scoped_cout_redirector& operator =(const scoped_cout_redirector& assign);

    std::streambuf* backup_;
    std::ofstream filestr_;
    std::streambuf* sbuf_;
};


int main()
{
    {
        scoped_cout_redirector file1("file1.txt");
        std::cout << "This is written to the first file." << std::endl;
    }

    std::cout << "This is written to stdout." << std::endl;

    {
        scoped_cout_redirector file2("file2.txt");
        std::cout << "This is written to the second file." << std::endl;
    }

    return 0;
}

Upvotes: 1

Jonathan M Davis
Jonathan M Davis

Reputation: 38267

Your overloaded << operator shouldn't care what kind of output stream that it goes to or whether it's appending or overwriting. It takes a std::ostream& and writes to it. If it's used with cout, then it's written to the console. If it's used with a std::ofstream, then it writes to a file. If that std::ofstream was opened to overwrite, then it overwrites. If it was opened to append, then it appends.

All that your overloaded << operator should care about is that it's writing to an output stream. What that stream represents is irrelevant.

Upvotes: 3

Shamim Hafiz - MSFT
Shamim Hafiz - MSFT

Reputation: 22064

This overloading is already done for the C++ file stream. The name cout means console out and the spirit is, it will output to console. One thing you could do is use freopen(), to redirect all output to a file.

Upvotes: 1

vitaut
vitaut

Reputation: 55524

Normally you provide std::ostream& operator<<(std::ostream&, const YourClass&) for your class and write to a file stream as follows:

std::ofstream ofs("out");
ofs << yourObject;

where yourObject is an object of YourClass.

Upvotes: 3

Related Questions