I'd like to overload << operator to write the value it takes to a file and cout. I have tried to do it with following code, but couldn't succeed it. It just writes the value to text file. Any help would be appreciated. Thank you.
void operator<<(std::ostream& os, const string& str)
{
std::cout << str;
os << str;
}
int main() {
ofstream fl;
fl.open("test.txt");
fl << "!!!Hello World!!!" << endl;
return 0;
}
See Question&Answers more detail:os