Return to site

Dev Null C++

broken image


Linux is an interesting operating system that hosts some virtual devices for numerous purposes. As far as programs running in the system are concerned, these virtual devices act as if they are real files. Tools can request and feed data from these sources. The data is generated by the OS instead of reading them from a disk.

One such example is /dev/null. It's a special file that's present in every single Linux system. However, unlike most other virtual files, instead of reading, it's used to write. Whatever you write to /dev/null will be discarded, forgotten into the void. It's known as the null device in a UNIX system.

Why would you want to discard something into the void? Let's check out what /dev/null is and its usage.

The C library function sizet strlen(const char.str) computes the length of the string str up to, but not including the terminating null character. Freopen('/dev/null', 'w', stdout); However, after the function call I cannot figure out how to reroute stdout to the console. I tried early in the program defining.

Prerequisites

Before diving deep into the usage of /dev/null, we have to have a clear grasp of the stdout and stderr data stream. Check out this in-depth guide on stdin, stderr, and stdout.

  1. C Object Oriented; C Advanced; Dev/null is used to instantly send EOF to the program, so that it doesn't wait for input (/dev/null, the null device, is a special file that discards all data written to it, but reports that the write operation succeeded, and provides no data to any process that reads from it, yielding EOF immediately).& is a special type of command separator used to.
  2. The null device is also a favorite subject of technical jokes, such as warning users that the system's /dev/null is already 98% full. The April Fool's, 1995 issue of the German magazine c't reported on an enhanced /dev/null chip that would efficiently dispose of the incoming data by converting it to a flicker on an internal glowing LED.

Let's have a quick refresh. Whenever any command-line utility is run, it generates two outputs. The output goes to stdout and the error (if generated) goes to stderr. By default, both these data streams are associated with the terminal.

For example, the following command will print out the string within the double quotation mark. Here, the output is stored in stdout.

The next command will show us the exit status of the previously-run command.

As the previous command ran successfully, the exit status is 0. Otherwise, the exit status will be different. What happens when you try to run an invalid command?

Now, we need to know about the file descriptor. In the UNIX ecosystem, these are integer values assigned to a file. Both stdout (file descriptor = 1) and stderr (file descriptor = 2) have a specific file descriptor. Using the file descriptor (1 and 2 in this situation), we can redirect the stdout and stderr to other files.

For starter, the following example will redirect the stdout of the echo command to a text file. Here, we didn't specify the file descriptor. If not specified, bash will use stdout by default.

The following command will redirect the stderr to a text file.

Using /dev/null

Dev null c++ v2

Redirecting output to /dev/null

Now, we're ready to learn how to use /dev/null. First, let's check out how to filter normal output and error. In the following command, grep will try to search for a string (hello, in this case) in the '/sys' directory.

However, it will generate a lot of error as without root privilege, grep can't access a number of files. In such a case, it'll result in 'Permission denied' errors. Now, using the redirection, we can get a clearer output.

The output looks much better, right? Nothing! In this case, grep doesn't have access to a lot of files and those that are accessible doesn't have the string 'hello'.

C++

In the following example, we'll be pinging Google.

Type

However, we don't want to see all those successful ping results. Instead, we only want to focus on the errors when ping couldn't reach Google. How do we do that?

Here, the contents of stdout are dumped to /dev/null, leaving only the errors.

Dev C++ Null Undeclared

Redirect all output to /dev/null

In certain situations, the output may not be useful at all. Using redirection, we can dump all the output into the void.

C++ Is Null

Let's break this command a little bit. First, we're dumping all the stdout to /dev/null. Then, in the second part, we're telling bash to send stderr to stdout. In this example, there's nothing to output. However, if you're confused, you can always check if the command ran successfully.

The value is 2 because the command generated a lot of errors.

If you tend to forget the file descriptor of stdout and stderr, the following command will do just fine. It's a more generalized format of the previous command. Both stdout and stderr will be redirected to /dev/null.

Other examples

This is an interesting one. Remember the dd tool? It's a powerful tool for converting and copying files. Learn more about dd. Using dd, we can test the sequential read speed of your disk. Of course, it's not an accurate measurement. However, for a quick test, it's pretty useful.

C++

Redirecting output to /dev/null

Now, we're ready to learn how to use /dev/null. First, let's check out how to filter normal output and error. In the following command, grep will try to search for a string (hello, in this case) in the '/sys' directory.

However, it will generate a lot of error as without root privilege, grep can't access a number of files. In such a case, it'll result in 'Permission denied' errors. Now, using the redirection, we can get a clearer output.

The output looks much better, right? Nothing! In this case, grep doesn't have access to a lot of files and those that are accessible doesn't have the string 'hello'.

In the following example, we'll be pinging Google.

However, we don't want to see all those successful ping results. Instead, we only want to focus on the errors when ping couldn't reach Google. How do we do that?

Here, the contents of stdout are dumped to /dev/null, leaving only the errors.

Dev C++ Null Undeclared

Redirect all output to /dev/null

In certain situations, the output may not be useful at all. Using redirection, we can dump all the output into the void.

C++ Is Null

Let's break this command a little bit. First, we're dumping all the stdout to /dev/null. Then, in the second part, we're telling bash to send stderr to stdout. In this example, there's nothing to output. However, if you're confused, you can always check if the command ran successfully.

The value is 2 because the command generated a lot of errors.

If you tend to forget the file descriptor of stdout and stderr, the following command will do just fine. It's a more generalized format of the previous command. Both stdout and stderr will be redirected to /dev/null.

Other examples

This is an interesting one. Remember the dd tool? It's a powerful tool for converting and copying files. Learn more about dd. Using dd, we can test the sequential read speed of your disk. Of course, it's not an accurate measurement. However, for a quick test, it's pretty useful.

$ ddif=<big_file>of=/dev/null status=progress bs=1M iflag=direct

Here, I've used the Ubuntu 18.04.4 ISO as the big file.

Similarly, you can also test out the download speed of your internet connection.

Final thoughts

Hopefully, you have a clear understanding of what this /dev/null file is. It's a special device that, if written to it, discards and if read from, reads null. The true potential of this interesting feature is in interesting bash scripts.

Are you interested in bash scripting? Check out the beginner's guide to bash scripting.

Enjoy!





broken image