472,954 Members | 1,448 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 software developers and data experts.

Can I redirect stdout to a file AND the console.

I wrote a program that writes a large amount of information to stdout
(and stderr). When run from the commandline, this output either appears
on the console window (the default) or can be redirected to a file by
means of the freopen() function:

freopen ("logfile.txt", "w", stdout);

But I would like to have the output on the console AND a logfile. Is
this possible (without using an extra program like 'tee')?
Nov 23 '07 #1
10 8292
Jef Driesen <je********@hotmail.com.invalidwrote:
I wrote a program that writes a large amount of information to stdout
(and stderr). When run from the commandline, this output either appears
on the console window (the default) or can be redirected to a file by
means of the freopen() function:

freopen ("logfile.txt", "w", stdout);

But I would like to have the output on the console AND a logfile. Is
this possible (without using an extra program like 'tee')?
Not without writing it twice.

Richard
Nov 23 '07 #2
In article <fi**********@ikaria.belnet.be>, Jef Driesen
<je********@hotmail.com.invalidwrote on Friday 23 Nov 2007 1:17 pm:
I wrote a program that writes a large amount of information to stdout
(and stderr). When run from the commandline, this output either
appears on the console window (the default) or can be redirected to a
file by means of the freopen() function:

freopen ("logfile.txt", "w", stdout);

But I would like to have the output on the console AND a logfile. Is
this possible (without using an extra program like 'tee')?
Yes. Duplicate your output to both stderr and to the file. You can use a
small "wrapper" function to encapsulate this functionality.

In any case you need to use two different file streams. You can't route
output to multiple devices by writing to the same stream within
Standard C. It might be possible with platform specific functions, but
you need to ask in a group appropriate for your system.

Nov 23 '07 #3
santosh said:
In article <fi**********@ikaria.belnet.be>, Jef Driesen
<je********@hotmail.com.invalidwrote on Friday 23 Nov 2007 1:17 pm:
<snip>
>>
But I would like to have the output on the console AND a logfile. Is
this possible (without using an extra program like 'tee')?

Yes. Duplicate your output to both stderr and to the file. You can use a
small "wrapper" function to encapsulate this functionality.
Something like this, perhaps (but see the note that follows):

#include <stdio.h>
#include <stdarg.h>

int tfprintf(FILE *fpa, FILE *fpb, const char *fmt, ...)
{
int rc = 0;
va_list ap = {0};
va_start(ap, fmt);
rc = vfprintf(fpa, fmt, ap);
va_end(ap);
if(rc >= 0)
{
va_start(ap, fmt);
rc = vfprintf(fpb, fmt, ap);
va_end(ap);
}
return rc;
}

This function's handling of the return values from the two vfprintf calls
is not particularly satisfactory, but it's hard to see how one could come
up with a solution that would be pleasing to everyone. Here's one
possibility:

int tfprintf(int *rcb, FILE *fpa, FILE *fpb, const char *fmt, ...)

(with the obvious changes within the function itself), so that the function
returns the value returned by the first vfprintf, and *rcb is populated
with the value returned by the second vfprintf.

Here's another solution:

struct tfprintf_rt_
{
int rca;
int rcb;
};

struct tfprintf_rt_ tfprintf(FILE *fpa, FILE *fpb, const char *fmt, ...)

And here's another:

#define TFPRINTF_BOTH_OK 0
#define TFPRINTF_FAIL1 1
#define TFPRINTF_FAIL2 2
#define TFPRINTF_BOTH_BAD (TFPRINTF_FAIL1 | TFPRINTF_FAIL2)

int tfprintf(struct tfprintf_rt_ *rc, FILE *fpa, FILE *fpb, const char
*fmt, ...)

with the return value giving a quick and dirty summary in bitflag form,
with the details stored in the struct for perusal if required.

But they all suck, really, don't they? It's just a matter of finding the
method that sucks *least*, whether it is one of these or some other
construction - and that's very much a personal style choice.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 23 '07 #4
in comp.lang.c i read:
>I wrote a program that writes a large amount of information to stdout
(and stderr).
>But I would like to have the output on the console AND a logfile. Is
this possible (without using an extra program like 'tee')?
in addition to leaving it to the person using your program to arrange for
it and the other response(s) (output twice), it may be worth noting that
the semantics of the filename parameter is implementation defined so it may
be possible without any more effort than the formulation of the argument
value. but i wouldn't count on it, and a strictly conforming program
cannot, so perhaps it is best to find another way.

--
a signature
Nov 24 '07 #5
those who know me have no need of my name wrote:
Jef Driesen wrote:
>I wrote a program that writes a large amount of information to
stdout (and stderr).
>But I would like to have the output on the console AND a logfile.
Is this possible (without using an extra program like 'tee')?

in addition to leaving it to the person using your program to
arrange for it and the other response(s) (output twice), it may
be worth noting that the semantics of the filename parameter is
implementation defined so it may be possible without any more
effort than the formulation of the argument value. but i
wouldn't count on it, and a strictly conforming program cannot,
so perhaps it is best to find another way.
Simple. Write the program with a 'putdouble' routine, that writes
a char to both of two separate files. Let the command structure
select those files. You can also arrange to have default names,
which may include stdout and null.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Nov 24 '07 #6
those who know me have no need of my name wrote:
in comp.lang.c i read:
>I wrote a program that writes a large amount of information to stdout
(and stderr).
>But I would like to have the output on the console AND a logfile. Is
this possible (without using an extra program like 'tee')?

in addition to leaving it to the person using your program to arrange for
it and the other response(s) (output twice), it may be worth noting that
the semantics of the filename parameter is implementation defined so it may
be possible without any more effort than the formulation of the argument
value. but i wouldn't count on it, and a strictly conforming program
cannot, so perhaps it is best to find another way.
The problem is that I have an application that needs to be tested by a
number of people and they have to send the results back to me. Therefore
I redirect stdout and stderr to a file that is easy to send. But that
way, nothing appears on the console which gives the impression there is
no progress.

The test persons are not always very skilled computer users, so I want
to make it as easy as possible to run the test. Therefore I want to
avoid to explain how to redirect output them self. Also my testers are
mostly running Windows and they do not have a 'tee' utility.
Nov 24 '07 #7
santosh wrote:
In article <fi**********@ikaria.belnet.be>, Jef Driesen
<je********@hotmail.com.invalidwrote on Friday 23 Nov 2007 1:17 pm:
>I wrote a program that writes a large amount of information to stdout
(and stderr). When run from the commandline, this output either
appears on the console window (the default) or can be redirected to a
file by means of the freopen() function:

freopen ("logfile.txt", "w", stdout);

But I would like to have the output on the console AND a logfile. Is
this possible (without using an extra program like 'tee')?

Yes. Duplicate your output to both stderr and to the file. You can use a
small "wrapper" function to encapsulate this functionality.
That requires rewriting my application (and all libraries it depends
on). That is something I wanted to avoid because I need it for testing.
In any case you need to use two different file streams. You can't route
output to multiple devices by writing to the same stream within
Standard C. It might be possible with platform specific functions, but
you need to ask in a group appropriate for your system.
Do you happen to know if it can be done on Windows? I can't find
anything, but I don't know what function to look after.
Nov 24 '07 #8
In article <FY***********************@phobos.telenet-ops.be>,
Jef Driesen <je********@hotmail.com.invalidwrote:
>those who know me have no need of my name wrote:
>in comp.lang.c i read:
>>I wrote a program that writes a large amount of information to stdout
(and stderr).
>>But I would like to have the output on the console AND a logfile. Is
this possible (without using an extra program like 'tee')?

in addition to leaving it to the person using your program to arrange for
it and the other response(s) (output twice), it may be worth noting that
the semantics of the filename parameter is implementation defined so it may
be possible without any more effort than the formulation of the argument
value. but i wouldn't count on it, and a strictly conforming program
cannot, so perhaps it is best to find another way.

The problem is that I have an application that needs to be tested by a
number of people and they have to send the results back to me. Therefore
I redirect stdout and stderr to a file that is easy to send. But that
way, nothing appears on the console which gives the impression there is
no progress.

The test persons are not always very skilled computer users, so I want
to make it as easy as possible to run the test. Therefore I want to
avoid to explain how to redirect output them self. Also my testers are
mostly running Windows and they do not have a 'tee' utility.
I Expect there is a simple solution to this...

http://bmrc.berkeley.edu/people/chaffee/expectnt.html

It's a little old (and, of course, completely OT [heh heh]), but it
works very well. I've used it for years.

Nov 24 '07 #9
Jef Driesen wrote:
>
.... snip ...
>
The test persons are not always very skilled computer users, so I
want to make it as easy as possible to run the test. Therefore I
want to avoid to explain how to redirect output them self. Also my
testers are mostly running Windows and they do not have a 'tee'
utility.
So write one. It isn't hard.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Nov 25 '07 #10
On Sat, 24 Nov 2007 19:29:41 GMT, Jef Driesen
<je********@hotmail.com.invalidwrote in comp.lang.c:
those who know me have no need of my name wrote:
in comp.lang.c i read:
I wrote a program that writes a large amount of information to stdout
(and stderr).
But I would like to have the output on the console AND a logfile. Is
this possible (without using an extra program like 'tee')?
in addition to leaving it to the person using your program to arrange for
it and the other response(s) (output twice), it may be worth noting that
the semantics of the filename parameter is implementation defined so it may
be possible without any more effort than the formulation of the argument
value. but i wouldn't count on it, and a strictly conforming program
cannot, so perhaps it is best to find another way.

The problem is that I have an application that needs to be tested by a
number of people and they have to send the results back to me. Therefore
I redirect stdout and stderr to a file that is easy to send. But that
way, nothing appears on the console which gives the impression there is
no progress.

The test persons are not always very skilled computer users, so I want
to make it as easy as possible to run the test. Therefore I want to
avoid to explain how to redirect output them self. Also my testers are
mostly running Windows and they do not have a 'tee' utility.
So why not give them a "tee" utility, and a batch file to use it to
invoke the program you want them to test? As Chuck pointed out, it's
not at all hard to write one. And there are almost certainly quite a
few freeware ones floating around for Windows.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Nov 25 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tsai Li Ming | last post by:
Dear all, I have a problem with a redirecting stdout and stderr. I am a top level module and has no control over the imported modules that are making system calls such as os.system or popen2.* ....
3
by: legba | last post by:
hi all.. I'm writing an application in python2.3 under linux debian which accepts new "plug-ins" in the form of c-written python extension modules. My problem is that I'd like to catch the...
10
by: Michael Gaab | last post by:
If I redirect stdout by using freopen("afile", "w", stdout); and then I closed stdout using, fclose(stdout), essentially I am just closing "afile". I have to reestablish what stdout originally...
2
by: 28tommy | last post by:
Hi there, I'm doing some TELNET and FTP sessions with my scripts. I need to redirect all the output (and not just what I print) to an output file, but still be able to see the session in process...
1
by: abcd | last post by:
I have a program which is written in C and interfaced with python via Swig. However, the function I call prints stuff out to the console. I would like to capture what it is printing out. I...
9
by: Santtu Nyrhinen | last post by:
Hi, Let say that I have a function like void writeHello() { printf("Hello"); } Now I need to make an automated test fot that function. The test function returns 1 for successful and 0 for...
3
by: mikem76 | last post by:
How do I automatically redirect stdout and stderr when using os.popen2 to start a long running process. If the process prints a lot of stuff to stdout it will eventually stop because it runs out...
6
by: cmk128 | last post by:
Hi here is my c file, compile in gcc 3.X in linux: #include <stdio.h> int main() { printf("Hello\n"); if (fork() == 0) printf("world! \n"); }
5
by: vedrandekovic | last post by:
Hello, e.g I need run my my_scripts_setup.bat that contain: python myscript_setup.py py2exe Can I cover or redirect log of that process into my wx program? I'am using Windows XP SP2, and...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
1
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.