The reason is the server programs that are executed by some RPC remote procedure call and has no loged session and could not be started from any unix session. I need to debug some of the such modules. I have a set of debuging-tools that writes to stdout. It is easy included into a source and present set of comfortable macro and function to be used for debuging a source. So, I would like to use it in that RPC-modules. I did this often in the past, but I am pretty hazy about it now.
You will need to check all the calls I suggest. One thing you have to do is synchronise the buffering. May not be strictly necessary if the stream has not been used yet, but best be safe. So before you mess with a stream, fflush it so there is no buffered data. The reason this works as far as I remember is that you just closed it, and open is guaranteed to use the lowest available fildes, so it should get the same number again.
If I am wrong about this quite possible , then look at the man page for dup2. This lets you transfer your new fildes to the lowest available fildes, if it does not work automatically. Thank you, guys, for your replies.
Paul, especial thanks to you: you are, as always, detailed and on deep level! Write character to stdout: how to use fputchar Write string to a stream: how to use fputs Write a block of data to a stream: how to use fwrite Save and write different type of value into file Save and read string into a file Write and read the entire array in one step.
Writing names and phone numbers to a file. Save text content into a file. Writing a file a character at a time. Read and write array into file. If stdout was an open file descriptor to a terminal preventing your ssh window from closing, such is life. The "right thing" to do if you really want to redirect "standard output", not just the stdio external variable "stdout", is to ensure that the file you want is opened specifically as file descriptor one, which is what dup2 does in the example you were given.
Which is much simpler than it looks once you realize it's redirecting twice: Once for stdout, once for stderr. Re-opening onto file descriptor one with dup2 also guarantees that whatever was there before, is forced to close.
This could be especially important if that happened to be a terminal or device file. Also, your example is concerning.
Never mix printf and cout, for starters, especially if you're going to play funny games with redirection. Last edited by Corona; at PM.. Find all posts by Corona Shell Programming and Scripting. Thanks Lucas 4 Replies. Hi All, I am using centOS. For example Hello I read a lot of post related to this topic, but nothing helped me. I need to check output for possible errors. Hi, I would like to avoid re-directing line by line to a file.
Thanks in advance. Cheers Vj 1 Reply. So, if I have a script script. I know I can do:. I run what i want, now i want undo this redirection. How to redirect stderr and stdout to a file.
0コメント