Solved

DOS redirect stdout and back again

Posted on 1997-06-06
6
535 Views
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");
            return(cdaInternalError);
      }
      retval = _spawnlp(_P_WAIT, "xcopy", "xcopy", inDir, outDir, "/E", "/I", "/H", "/Y", NULL);
      if (retval)
      {
            printf("xcopy error %i\n", retval);
            retval = cdaInternalError;
      }
      else
            retval = cdaSuccess;

      fclose(newStdout);
      return(retval);
}

--- end of code sample ---

This code works but the stdout is lost when the function returns.
0
Comment
Question by:icd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:LucHoltkamp
ID: 1251005
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);
system(buffer);

Good Luck
.luc.
0
 
LVL 5

Author Comment

by:icd
ID: 1251006
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.

0
 
LVL 2

Accepted Solution

by:
mlev earned 50 total points
ID: 1251007
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).
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:icd
ID: 1251008
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?
0
 
LVL 2

Expert Comment

by:mlev
ID: 1251009
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);
...
...
fflush(stdout);
dup2(old_stdout_fd, 1);
close(old_stdout_fd);
0
 
LVL 5

Author Comment

by:icd
ID: 1251010
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 :-)

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
logging Access violation 6 24
A way to create a macro table?  (or: a #define that expands to multiple #defines) 6 62
sameEnds challenge 3 195
What is atomic operation? 6 74
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question