• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

i need help with process standard output redirectioning

my application calls a process that wraps a dos executable, i want the process to put the output (standard and errors) into a text file and not in the screen; ive read about the STARTUPINFO structure and i use the hStdOut and the hStdErr members but they are not working properly. Please send me an example if possible,

thanxALot, david
0
davidzor
Asked:
davidzor
  • 3
  • 3
  • 2
  • +5
1 Solution
 
ExceterCommented:
Have you tried piping the output to file? Try calling the program like this,

MyProg > output.txt

Exceter
0
 
davidzorAuthor Commented:
thanx exceter, i tried that but my application takes commandline params. and your suggestion doesnt work out fine, am i doing something wrong?
0
 
ExceterCommented:
>> i tried that but my application takes commandline params. and your suggestion doesnt work out fine

That should not make any difference. Try this,

Myprog -d -h 45 > output.txt

Note: This sends all output to the file. If the program requires input from the user it will simply sit there, with nothing on the screen, until the data is typed in.

Exceter
0
Industry Leaders: 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!

 
davidzorAuthor Commented:
thanx again exceter, i was typing it wrong, but using the '>' causes the output to be destructive, when i run twice or more times my program only the last output remains in the .txt, do you know how to make it append instead of destroying the former contents of the file?

Do i need to use an API function or can i use an easy method like the ">"?

Ahh... i forgot.. my application is intended 2 work on NT, dont know if that makes any difference.

:) exècting to dont bother u so much, david
0
 
ExceterCommented:
>> do you know how to make it append instead of destroying the former contents of the file?

I'm afraid not.

Exceter
0
 
jcaldwelCommented:
 FILE *freopen(const char *path, const char *mode, FILE *stream) in stdio.h.

As the FILE*... pass stdout.

0
 
akshayxxCommented:
follow this discussion .. this will help you on redirection and then coming back to stdout..

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20420198.html
0
 
sarda_rameshCommented:
As mentioned by Exceter
1.The output can be redirected to another file like

exe > outputfile

2. if u want to append to the earlier file try ..

exe >>outputfile ..

3. u can also redirect errors like

exe >> outputfile 2>>errfile

and it works fine on buth UNIX or DOS based platforms

Regards
Ramesh

0
 
jcaldwelCommented:
Let me clarify my suggestion with some code...

#define LOGFILE "mylog.txt"

// The w+ tells it to append
stdout = freopen( LOGFILE, "w+", stdout );


Or to save off stdout,


#define LOGFILE "mylog.txt"
FILE* savestdout;

savestdout = stdout;

// The w+ tells it to append
stdout = fopen( LOGFILE, "w+" );

// So some stuff with stdout...

stdout = savestdout;

printf( "Now I Am back to the terminal\n" );




0
 
adijaCommented:
I think what you are trying ot do is something like this:

The main program makes a log file, then calls some dos executable, and you want the dos exe to put the errors in log.

There are 2 things:
1. In unix, there's a exec command, I don't know if it works on dos. In this, you first fork() the main program, then redirect the child's stderr to the log file  by
"dup" command, and exec() to start the exe with sh shell.
This way the error will go to the file.

2. In dos mode, you can very well use the system command, which is also availbale in C.
   System("<exe> 2>>log.txt");
0
 
jcaldwelCommented:
<from davidzor>
application is intended 2 work on NT
</from davidzor>

UNIX Does make life easier for a programmer.

If it can be helped, I would avoid using cmd.exe to redirect output.

I am more of a UNIX programmer by trade, so forgive me if this answer has been infected by my UNIX experience, but couldn't you reopen stdout/stderr programatically, and then use system to call the program. Wouldn't the reopened std/in/err be inherited by the command executed by system?
0
 
jmcgOwnerCommented:
Nothing has happened on this question in more than 10 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
PAQ, forfeit points (eventually abandoned by asker; some interesting responses, but I don't see asker's question as having been answered).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0
 
moduloCommented:
PAQed - no points refunded (of 50)

modulo
Community Support Moderator
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now