By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,916 Members | 1,728 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,916 IT Pros & Developers. It's quick & easy.

How to distinguish between stdout and stderr

P: n/a
Hi,

I have a program that sends some output to stdout and some to stderr. I
need to separate the two using the command-line so that I direct stderr
output to a file, say fileA.txt, and stdout output to a file, say
fileB.txt. I'm trying to implement a program that would then take the
two files and use them separately. By the way, I'm on Linux. Thanks!

-Andre

Nov 13 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Sun, 20 Jul 2003 17:22:14 +1000, Andre wrote:
Hi,

I have a program that sends some output to stdout and some to stderr.
I need to separate the two using the command-line so that I direct
stderr output to a file, say fileA.txt, and stdout output to a file,
say fileB.txt. I'm trying to implement a program that would then take
the two files and use them separately. By the way, I'm on Linux.
Thanks!

-Andre

OT.
But since it's such a small question I'll answer it anyway :P

#split stderr and stdout into seperate files
../myprog 1> stdout.file 2> stderr.file
#join stderr and stdout into the same file
../myprog &> stdout_and_stderr.file
--
main(int c,char*k,char*s){c>0?main(0,"adceoX$_k6][^hn","-7\
0#05&'40$.6'+).3+1%30"),puts(""):*s?c=!c?-*s:(putchar(45),c
),putchar(main(c,k+=*s-c*-1,s+1)):(s=0);return!s?10:10+*k;}
Nov 13 '05 #2

P: n/a
In 'comp.lang.c', Andre <fo********@hotmail.com> wrote:
Hi,

I have a program that sends some output to stdout and some to stderr. I
need to separate the two using the command-line so that I direct stderr
The command line trick is system-dependent.
output to a file, say fileA.txt, and stdout output to a file, say
fileB.txt. I'm trying to implement a program that would then take the
two files and use them separately. By the way, I'm on Linux. Thanks!


To stay portable, use freopen() at the very beginning of main().

if (freopen("fileB.txt", "w", stdout) != NULL)
{
if (freopen("fileA.txt", "w", stderr) != NULL)
{
/* your app. */
}
}

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #3

P: n/a
On Sun, 20 Jul 2003 17:22:14 +1000,
Andre <fo********@hotmail.com> wrote
in Msg. <3F**************@hotmail.com>
I have a program that sends some output to stdout and some to stderr. I
need to separate the two using the command-line so that I direct stderr
output to a file, say fileA.txt, and stdout output to a file, say
fileB.txt.


This problem is entirely a shell issue (on Unix), and a trivial one-liner
as such, but dependent on the shell you're using. It has nothing to do
with the C programming language -- and therefore doesn't belong in this
group, even though both the program in question and the shell are likely
written in C. The nice folks over at comp.unix.programmer will happily
help you.

--
"With me is nothing wrong! And with you?" (from r.a.m.p)
Nov 13 '05 #4

P: n/a
Hi Pieter,

Thank you so much :) you helped a great deal!

regards,

-Andre

Pieter Droogendijk wrote:
On Sun, 20 Jul 2003 17:22:14 +1000, Andre wrote:
Hi,

I have a program that sends some output to stdout and some to stderr.
I need to separate the two using the command-line so that I direct
stderr output to a file, say fileA.txt, and stdout output to a file,
say fileB.txt. I'm trying to implement a program that would then take
the two files and use them separately. By the way, I'm on Linux.
Thanks!

-Andre


OT.
But since it's such a small question I'll answer it anyway :P

#split stderr and stdout into seperate files
../myprog 1> stdout.file 2> stderr.file
#join stderr and stdout into the same file
../myprog &> stdout_and_stderr.file


Nov 13 '05 #5

P: n/a
in comp.lang.c i read:
On Sun, 20 Jul 2003 17:22:14 +1000,
Andre <fo********@hotmail.com> wrote
in Msg. <3F**************@hotmail.com>
I have a program that sends some output to stdout and some to stderr.

This problem is entirely a shell issue (on Unix), and a trivial one-liner
as such, but dependent on the shell you're using. It has nothing to do
with the C programming language -- and therefore doesn't belong in this
group, even though both the program in question and the shell are likely
written in C. The nice folks over at comp.unix.programmer will happily
help you.


actually comp.unix.shell would be better.

programmatically, one might use freopen() to associate different files to
each stream, from within the program, an issue appropriate for either
comp.lang.c or comp.unix.programmer, though outside of the initial request.

--
a signature
Nov 13 '05 #6

P: n/a
In <Xn***************************@130.133.1.4> Emmanuel Delahaye <em**********@noos.fr> writes:
In 'comp.lang.c', Andre <fo********@hotmail.com> wrote:
Hi,

I have a program that sends some output to stdout and some to stderr. I
need to separate the two using the command-line so that I direct stderr


The command line trick is system-dependent.
output to a file, say fileA.txt, and stdout output to a file, say
fileB.txt. I'm trying to implement a program that would then take the
two files and use them separately. By the way, I'm on Linux. Thanks!


To stay portable, use freopen() at the very beginning of main().

if (freopen("fileB.txt", "w", stdout) != NULL)
{
if (freopen("fileA.txt", "w", stderr) != NULL)
{
/* your app. */
}
}


You got it *completely* wrong!

What makes you think that fileB.txt and fileA.txt are *portable* file
names? By hardcoding file names in your code, you have given up any
hope of portability and the program itself is horribly inflexible: what
if you want to redirect to other files on the next run?

The right thing is to handle these details from outside the program,
so that the program's code remains portable and the program itself is
flexible.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #7

P: n/a
In <20*************************@binky.homeunix.org> Pieter Droogendijk <gi*@binky.homeunix.org> writes:
On Sun, 20 Jul 2003 17:22:14 +1000, Andre wrote:

I have a program that sends some output to stdout and some to stderr.
I need to separate the two using the command-line so that I direct
stderr output to a file, say fileA.txt, and stdout output to a file,
say fileB.txt. I'm trying to implement a program that would then take
the two files and use them separately. By the way, I'm on Linux.
Thanks!
OT.
But since it's such a small question I'll answer it anyway :P

#split stderr and stdout into seperate files
./myprog 1> stdout.file 2> stderr.file


fangorn:~ 1780> ls 1> stdout.file 2> stderr.file
Ambiguous output redirect.
#join stderr and stdout into the same file
./myprog &> stdout_and_stderr.file


fangorn:~ 1781> ls &> stdout_and_stderr.file
Invalid null command.

What am I doing wrong? ;-)

Don't attempt to answer any question, topical or not, if you don't know
the *complete* answer!

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.