?
Solved

i need help with process standard output redirectioning

Posted on 2003-03-04
13
Medium Priority
?
245 Views
Last Modified: 2010-04-15
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
Comment
Question by:davidzor
[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
  • 3
  • 2
  • +5
13 Comments
 
LVL 8

Expert Comment

by:Exceter
ID: 8066916
Have you tried piping the output to file? Try calling the program like this,

MyProg > output.txt

Exceter
0
 

Author Comment

by:davidzor
ID: 8067005
thanx exceter, i tried that but my application takes commandline params. and your suggestion doesnt work out fine, am i doing something wrong?
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8067084
>> 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
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!

 

Author Comment

by:davidzor
ID: 8068240
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
 
LVL 8

Expert Comment

by:Exceter
ID: 8068436
>> do you know how to make it append instead of destroying the former contents of the file?

I'm afraid not.

Exceter
0
 
LVL 1

Expert Comment

by:jcaldwel
ID: 8068828
 FILE *freopen(const char *path, const char *mode, FILE *stream) in stdio.h.

As the FILE*... pass stdout.

0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8069227
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
 
LVL 1

Expert Comment

by:sarda_ramesh
ID: 8078335
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
 
LVL 1

Expert Comment

by:jcaldwel
ID: 8080372
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
 

Expert Comment

by:adija
ID: 8097665
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
 
LVL 1

Expert Comment

by:jcaldwel
ID: 8103445
<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
 
LVL 20

Expert Comment

by:jmcg
ID: 10089663
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
 

Accepted Solution

by:
modulo earned 0 total points
ID: 10136136
PAQed - no points refunded (of 50)

modulo
Community Support Moderator
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

752 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