• C

DOS redirect stdout and back again

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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
icdAuthor Commented:
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).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
How do you know if your security is working?

Protecting your business doesn’t have to mean sifting through endless alerts and notifications. With WatchGuard Total Security Suite, you can feel confident that your business is secure, meaning you can get back to the things that have been sitting on your to-do list.

icdAuthor Commented:
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);
icdAuthor Commented:
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 :-)

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.