Question

Suppose I've got code like

#include <iostream>
using namespace std;

int main() {
    cout << "Redirect to file1" << endl;
    cout << "Redirect to file2" << endl;
    return 0;
}

I want to redirect the first output to file1 and the second to file2. Is that possible?

I think in C, fclose(stdout) and reopen the stdout might help but I'm not sure how to reopen it or whether it works.

Thanks

UPDATE: What for?

I have a program A, which reads input from the user and generates corresponding output. Now I want to check whether it is correct, I have a program B which generate input for A, as well as correct output. B will generate one set of test data at a time. And I will have thousands of tests.

On my machine, a thousand times ./B > ``mktemp a.XXX`` works better than using ofstream. Using fstream for thousands of times, my hard drive light flashes crazily. But not when redirecting to temp file.

UPDATE2:

In C++, it seems that the prevailing answer is cout along with cerr.

What about C, apart from stderr, can I close stdout and reopen it?

Was it helpful?

Solution 3

You can always use the standard error stream for e.g. error messages.

#include <iostream>
using namespace std;

int main() {
    cout << "Redirect to file1" << endl;
    cerr << "Redirect to file2" << endl;
}

For example, using the Windows [cmd.exe] command interpreter, and the Visual C++ cl compiler:

[D:\dev\test]
> type con >streams.cpp
#include <iostream>
using namespace std;

int main() {
    cout << "Redirect to file1" << endl;
    cerr << "Redirect to file2" << endl;
}
^Z

[D:\dev\test]
> cl streams.cpp
streams.cpp

[D:\dev\test]
> streams 1>a.txt 2>b.txt

[D:\dev\test]
> type a.txt
Redirect to file1

[D:\dev\test]
> type b.txt
Redirect to file2

[D:\dev\test]
> _


EDIT: added colorized code and boldface emphasis.

OTHER TIPS

Why not use file streams ? this way it will most likely work regardless of the shell redirection:

#include <fstream>
#include <fstream>
using namespace std;
// opeen files
ofstream file1 ( "file1");
ofstream file2 ( "file2");
//write
file1 << "Redirect to file1" << endl;
file2 << "Redirect to file2" << endl;
//close files
file1.close();
file2.close();

You can use cout AND cerr.

cout << "Redirect to file1" << endl;
cerr << "Redirect to file2" << endl;

cerr goes to standard error

Another way doing it is using cout.rdbuf() like this:

#include <iostream>
#include <fstream>
using namespace std;

int main () {
    ofstream cfile1("test1.txt");
    ofstream cfile2("test2.txt");

    cout.rdbuf(cfile1.rdbuf());        
    cout << "Redirect to file1" << endl;

    cout.rdbuf(cfile2.rdbuf());        
    cout << "Redirect to file2" << endl; 

    return 0;
}

Code:

#include <iostream>
using namespace std;

int main() {
    cout << "Redirect to file1" << endl;
    cerr << "Redirect to file2" << endl;
    return 0;
}

Console:

test > 1.txt 2> 2.txt

1.txt:

Redirect to file1

2.txt:

Redirect to file2
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top