Redirect To Dev Null C

  1. Unix Redirect Output To Null
  2. Redirect Stdout To Dev Null C

ikeaboy> Hello.
ikeaboy> I wonder if this is the right way to redirect stdout, stderr
ikeaboy> and stdin to /dev/null:

ikeaboy> freopen('/dev/null', 'w', stdout);
ikeaboy> freopen('/dev/null', 'w', stderr);
ikeaboy> freopen('/dev/null', 'r', stdin);

Redirecting to /dev/null won't prevent crashing, but will clean up the stdout and stderr output streams. Tar could cause errors in a variety of ways. You might not have write access, the file might already exist, etc. – Sparhawk Mar 14 '14 at 4:27. Just a trick to avoid unnecessary output. Dev and c redirect stdout stdin how to control popen stdin, stdout, stderr redirection? How to redirect output to a file and stdout. To discard standard output completely on Linux, redirect the standard output to /dev/null. Redirecting to /dev/null causes data to be completely discarded and erased. $ cat file /dev/null. Note: redirecting to /dev/null does not erase the content of the file but it only discards the content of the standard output. Or, you can redirect the output to one place, and the errors to another. Dir file.xxx output.msg 2 output.err You can print the errors and standard output to a single file by using the '&1' command to redirect the output for STDERR to STDOUT and then sending the output from STDOUT to a file.

ikeaboy> Is the mode correct ? Would 'w+' be better ? How to mix with traktor pro 2.

ikeaboy> Also, in lot of open source code is this:

ikeaboy> close(0); close(1); close(2);
ikeaboy> open('/dev/null',O_RDWR); dup(0); dup(0);

ikeaboy> Which approach is better and more portable ?

The first approach isn't guaranteed to work, because freopen() isn't
guaranteed to reuse the same underlying file descriptors for the new
stream. You may find it works anyway as a result of pure coincidence.
If the same descriptors _aren't_ reused, then stdio functions used to
write to 'stderr' will write to /dev/null, but POSIX writes to
STDERR_FILENO will not (and will possibly go to some completely
unexpected place if you're really unlucky). (Note that some library
functions can write to standard error, and in some cases this means
STDERR_FILENO rather than 'stderr'.)

Unix Redirect Output To Null

Unix

Redirect Stdout To Dev Null C

The second approach avoids this problem. However, a slightly safer
way of doing it is:

int fd = open('/dev/null',O_RDWR);
/* handle failure of open() somehow */
dup2(fd,0);
dup2(fd,1);
dup2(fd,2);
if (fd > 2)
close(fd);

This has the same effect as the code you posted, but has the advantage
of being thread-safe. (relying on open()/dup() to pick the right
descriptor numbers is only safe if there are no other threads running)

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>

Comments are closed.