How to write in stdout after using freopen [duplicate]
Asked Answered
C

2

2

After freopen-ing stdout, How can I print on terminal?

freopen("out", "w", stdout); // reopen stdout
/* something */
printf("Now I want to print this on terminal");
Closestool answered 2/11, 2014 at 12:57 Comment(7)
Code is easier to understand than English - you should post it.Case
On what OS? I believe Windows doesn't necessarily allow this to work...Otranto
@MatsPetersson on the ubuntuClosestool
Then post a small example of what you are doing, and explain what you expect vs. what actually happen.Otranto
@darkdream Do you want to print something in file then something in console?Mess
@AramGevorgyan yes...Closestool
I completely edit your question. Please make sure my edit is exactly what you want.Juliusjullundur
S
8

I believe this is what you are looking for:

Once I've used freopen, how can I get the original stdout (or stdin) back?

There's no portable solution. But the link also explains a possible solution using your own stream and a non-portable solution that'll work on most posix systems.

There isn't a good way. If you need to switch back, the best solution is not to have used freopen in the first place. Try using your own explicit output (or input) stream variable, which you can reassign at will, while leaving the original stdout (or stdin) undisturbed. For example, declare a global

FILE *ofp;

and replace all calls to printf( ... ) with fprintf(ofp, ... ). (Obviously, you'll have to check for calls to putchar and puts, too.) Then you can set ofp to stdout or to anything else.

You might wonder if you could skip freopen entirely, and do something like

FILE *savestdout = stdout;
stdout = fopen(file, "w");    /* WRONG */

leaving yourself able to restore stdout later by doing

stdout = savestdout;      /* WRONG */

but code like this is not likely to work, because stdout (and stdin and stderr) are typically constants which cannot be reassigned (which is why freopen exists in the first place).

It may be possible, in a nonportable way, to save away information about a stream before calling freopen to open some file in its place, such that the original stream can later be restored. The most straightforward and reliable way is to manipulate the underlying file descriptors using a system-specific call such as dup or dup2, if available. Another is to copy or inspect the contents of the FILE structure, but this is exceedingly nonportable and unreliable.

Under some systems, you might be able to reopen a special device file (such as /dev/fd/1 under modern versions of Unix) which is still attached to (for example) the original standard output. You can, under some systems, explicitly re-open the controlling terminal, but this isn't necessarily what you want, since the original input or output (i.e. what stdin or stdout had been before you called freopen) could have been redirected from the command line.

Silvanasilvano answered 2/11, 2014 at 13:19 Comment(0)
M
-4

You can do it by:

#include <fstream> 

ofstream out("out.txt");

out<<"something";

then

cout<<"something";
Mess answered 2/11, 2014 at 13:16 Comment(3)
This not answering the questionTarrasa
@SHR: Let the man who ask this question to say it is answering or not, may be it's usefull for himMess
If you're going to write an answer like this, at least use stdio to be consistent with the question. Otherwise we may have a case of "Q: How do I do this in Java?" "A: Use C# instead."Directional

© 2022 - 2024 — McMap. All rights reserved.