How to distinguish between stdout and stderr

A

Andre

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
 
P

Pieter Droogendijk

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
 
E

Emmanuel Delahaye

In 'comp.lang.c' said:
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. */
}
}
 
D

Daniel Haude

On Sun, 20 Jul 2003 17:22:14 +1000,
in Msg. said:
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.
 
T

those who know me have no need of my name

in comp.lang.c i read:
On Sun, 20 Jul 2003 17:22:14 +1000,
in Msg. <[email protected]>
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.
 
D

Dan Pop

In said:
The command line trick is system-dependent.


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
 
D

Dan Pop

In said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top