0

What the question says.

Ultimately what I want is to execute gcc and capture the output if there's an error. The problem is errors are written to stderr instead of stdout. On Linux I can do

gcc foo.c 2>&1

How can I accomplish this on Windows?

3
  • 1
    Actually, the question says almost nothing. What is "the Windows prompt?" Commented Aug 11, 2010 at 16:04
  • You want something like a text screenshot of whatever is showing in a command prompt? Commented Aug 11, 2010 at 16:05
  • Re-wrote the question for clarity. Commented Aug 11, 2010 at 16:13

3 Answers 3

5

There is. Simply right click into the console window, select Mark. With your mouse select the desired area and right click. Now you can paste it into a text file with Ctrl-V.

If you need the output of a program into a text file, run it like this:

myprogram.exe > myfile.txt

See here about redirecting:
1. Using command redirection operators
2. Redirecting Error Messages from Command Prompt: STDERR/STDOUT

You can do what you want like this: D:\>dir 1> test.txt 2> testerr.txt

Sign up to request clarification or add additional context in comments.

5 Comments

I tried that. But, at the command prompt, the output to appear ... But the output is not displayed when pointing to a text file.
your program/script will have to get that info from the text file. There is a util somewhere called 'tee' that redirects stdout to a file while still showing it on the console.
Yeah, is it. Thanks... How its works? Why i can't get the output just with: gcc > myfile.txt Why i need to put 2, this way? gcc 2> myfile.txt
@Richard: > redirection is the same as 1> (which is stdout redirection). 2> redirection is stderr redirection. All-in-all, this is exactly the same as in Linux.
@all: I'm puzzled this answer got so many upvotes. The correct and short answer to question How do I do the same in Windows? should have been: You do it the same way as in Linux.
4

Richard, your "accepted answer" is too long and it is too wrong.

The short answer to your question (as currently stated in your last sentence: "How can I accomplish this on Windows?") is:

Exactly like you do it on Linux!



But I'll also give you a long answer.

Your 2>&1 redirection works in a cmd.exe window the same way. I even re-tested it right now, since my cmd.exe experience is a bit rusty. I used this Ghostscript command (intentionally meant to produce output on stdout as well as on stderr):

gswin32c -sDEVICE=nullpage -dFirstPage=12 -dLastPage=11 my-20-page-test.pdf

I got all the expected output into the shell window. Then I did:

gswin32c -sDEVICE=nullpage -dFirstPage=12 -dLastPage=11 my-20-page-test.pdf ^
          1>stdout.log

and stderr still printed into the window, but stdout.log had the 'missing' original output. Next I did:

gswin32c -sDEVICE=nullpage -dFirstPage=12 -dLastPage=11 my-20-page-test.pdf ^
          2>stderr.log

and stdout now printed into window, while stderr.log had the rest of Ghostscript's messages. Next:

gswin32c -sDEVICE=nullpage -dFirstPage=12 -dLastPage=11 my-20-page-test.pdf ^
          1>stdout.log 2>stderr.log

and (as expected): no output in window, all output divided up between stdout.log and stderr.log. Last test:

gswin32c -sDEVICE=nullpage -dFirstPage=12 -dLastPage=11 my-20-page-test.pdf ^
          1>all.log 2>&1

and result now:

  1. nothing in window,
  2. everything in all.log.

Which is the same behaviour as stderr/stdout redirection as on Linux.

1 Comment

To be fair, the question looks like that because I edited it. On the other hand, this is what it looked like before I did it.
0

If you want the output of a particular command, there's a simple way to push console output to a file.

Here's a trivial example using the 'dir' command (the leading > represents your prompt):

>dir > diroutput.txt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.