What I am trying to do is redirect all my cout to a file, rather than a console. I am using a gcc complier to run my program. It works well when I do

./a.out > output.txt.

But i was wondering is there a way i can do the same thing but inside my code, that will call all my cout to a file?

use ofstream class and write to file.

#include <ofstream>

using namespace std;

int main(int argc, char *argv[]) {
  ofstream ofs("test.txt");
  ofs << "Hello there, file" << endl;
  ofs.close();
}

something like that maybe?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.