How To Redirect Stdout To A File In C++


ulula

Still Fresh
Joined
Jan 9, 2009
Messages
41
Age
42
Location
amiens
i know the command line in bash but how to do it in c++ ?
i don't want to use "fprintf" instead of "printf"

CODE

...
$log=log.txt
&>> $log # redirect stdout and stderr to log
...
 
i had find a solution
<_< that may be not the best way to redirect stdout

CODE

#include <stdio.h>

int main ()
{
FILE* log, original_stdout;
// create a log file
log = fopen ("log.txt","wa");
if (log == NULL)
{
printf("Unable to create log");
return -1;
}

//swap stdout with log
original_stdout = stdout;
stdout = log

//append to log
printf("Hello world");

//restore stdout and close log
stdout = original_stdout;
fclose(log);

return 0;
}
 
You need to open a file and use dup2 to close STDOUT and replace it with your log file.

Here's an example (no error checking included).

I've used cout (printf is so C :)).

CODE
#include <iostream>
#include <unistd.h>
#include <fcntl.h>

using namespace std;

int main(int, char**)
{
int fd = open("log.txt", O_WRONLY|O_CREAT|O_TRUNC, 0644);
dup2(fd, STDOUT_FILENO);

cout << "Stuff to log" << endl;

close(fd);

return 0;
}
 
;

return 0;
}
I already try this code. It's not work on my wiz 1.0.0.

Parkydr posted on May 25 2009 at 03:38 AM said:
You need to open a file and use dup2 to close STDOUT and replace it with your log file.
Here's an example (no error checking included).

I've used cout (printf is so C :)).

CODE
#include <iostream>
#include <unistd.h>
#include <fcntl.h>

using namespace std;

int main(int, char**)
{
int fd = open("log.txt", O_WRONLY|O_CREAT|O_TRUNC, 0644);
dup2(fd, STDOUT_FILENO);

cout << "Stuff to log" << endl;

close(fd);

return 0;
}

This code is work after you boot the wiz only.It may not restore the stdout after return.

Squidge posted on May 25 2009 at 04:27 AM said:
why not just:
freopen ("myfile.txt","w",stdout);

?

Can you post more code? I can not make this work on my wiz 1.0.0.

I need this redirect function because my wiz does not have serial. Now I am blind programmer :)
 
Last edited by a moderator:
If you want to redirect the output of your program into a file, just try this:

CODE
#!/bin/sh

"./yourapp.gpe" >stdout.txt 2>stderr.txt
cd /usr/gp2x
./gp2xmenu


Copy this into a file (e.g. wrapper.gpe) and save it in the same directory as your program. Make it executable using:

CODE
chmod +x wrapper.gpe


Then execute it. After this, you can see two new files, called stdout.txt and stderr.txt in the same directory. I've tested it and it worked for me.
 
Assuming the wiz works like the GP2X, you don't need to do any of the above, just write a script that runs your program redirecting stdout as in your original post.

Edit: Jan-Nik beat me to it
 
If you want it for debugging, why not use something like:

CODE
#define DEBUG

#ifdef DEBUG
#define dbgprintf(format, args...) fprintf (stderr, format , ## args)
#else
#define dbgprintf(format, args...)
#endif


That way, for a release version, you can turn off all debugging output by just commenting out the DEBUG macro:

CODE
//#define DEBUG


Also, your debugging messages come out of a different stream than your usual output, and so can be filtered appropriately. Of course, the above can be changed to output to any file, string, or anything else. Just change the macro. Using it is exactly the same as printf, apart from the 'dbg' prefix.
 
Jan-Nik posted on May 25 2009 at 10:41 AM said:
If you want to redirect the output of your program into a file, just try this:

CODE
#!/bin/sh

"./yourapp.gpe" >stdout.txt 2>stderr.txt
cd /usr/gp2x
./gp2xmenu
Copy this into a file (e.g. wrapper.gpe) and save it in the same directory as your program. Make it executable using:

CODE
chmod +x wrapper.gpe


Then execute it. After this, you can see two new files, called stdout.txt and stderr.txt in the same directory. I've tested it and it worked for me.

Note that if you're a Windows user, the above wrapper.gpe must be in Unix format, not MSDOS format. If it's in MSDOS format the script file will just hang. Use an editor like Notepad++ that's capable of saving the file in the correct format.
 
Last edited by a moderator:
satacoy said:
[...]
Note that if you're a Windows user, the above wrapper.gpe must be in Unix format, not MSDOS format. If it's in MSDOS format the script file will just hang. Use an editor like Notepad++ that's capable of saving the file in the correct format.

D'oh. Thanks heaps for pointing that out.
 
Last edited by a moderator:
Back
Top