We help IT Professionals succeed at work.

DOS redirect stdout and back again

icd asked
Medium Priority
Last Modified: 2008-03-03
I want to spawn a program (XCOPY) having directed stdout to a file for log purposes. I can do the re-direction but I lose stdout once I close the file handle. How can I recover the original stdout?

---- code sample ----
PUBLIC CdaRETVAL      ClCopyFileStructure(PCdaCHAR inDir, PCdaCHAR outDir)
      CdaRETVAL      retval;                                          // Return value from function
      FILE            *newStdout;                                    // Handle for re-directed stdout
      newStdout = freopen("c:\\temp.tmp", "w", stdout);
      if (newStdout == NULL)
            printf("Cannot redirect stdout\n");
      retval = _spawnlp(_P_WAIT, "xcopy", "xcopy", inDir, outDir, "/E", "/I", "/H", "/Y", NULL);
      if (retval)
            printf("xcopy error %i\n", retval);
            retval = cdaInternalError;
            retval = cdaSuccess;


--- end of code sample ---

This code works but the stdout is lost when the function returns.
Watch Question

You could simply use the function
system(char* cmdLine)
which executes the string in cmdLine.

char buffer[256];
sprintf(buffer, "xcopy /e/i/h/y %s %s > c:\\temp.tmp", inDir, outDir);

Good Luck


That would be acceptable if I only had a little more output to generate but I have lots, including spawning further programs so I really want to get stdout back again.

Do not close the file handle.

freopen() does not create a new FILE structure, it just "alters" the existing one. The only useful thing you can do with the return value of freopen(), is check whether the function succeeded (otherwise it's NULL). If it did, newStdout will be equal to stdout, so your program actually performs fclose(stdout).

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


OK, it only 'alters' the existing file structure. So if I don't close the newStdout how do I pursuade stdout to go back to the console. Do I do a freopen("con:" or somesuch?

I guess that's the simplest way, if you do not care about your program being run with redirected output, or even compiled on something other than DOS.

I think the portable way to do it should be something like:

old_stdout_fd = dup(1);
freopen("c:\\temp.tmp", "w", stdout);
dup2(old_stdout_fd, 1);


Sorry for the delay I got pushed off one project onto another before I could evaluate your answer.

I just got in before the 15 day deadline so you get full 200 points rather than 150 :-)

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.