DOS redirect stdout and back again
Posted on 1997-06-06
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);
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.