/dev/zero is a special file in Unix-like operating systems that provides as many null characters (ASCII NUL, 0x00) as are read from it. One of the typical uses is to provide a character stream for initializing data storage. If str is NULL: The call is considered as subsequent calls to strtok and the function continues from where it left in previous invocation. It is defined in header file. Strtok Parameters. Str: Pointer to the null terminated byte string to tokenize. Delim: Pointer to the null terminated byte string that contains the separators. /dev/null is the null device it takes any input you want and throws it away. It can be used to suppress any output. Note that file 2&1 is an older syntax which still works, & file is neater, but would not have worked on older systems.

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.

Dev Null C++ V2

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

C++

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.

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?

Dev C++ Null Undeclared

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

Redirecting output to /dev/null

Dev Null C++

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”.

Dev Null C++ Mysql

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.

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.

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.

Dev c++ null undeclared

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

Computer tipspsp unlimited speed

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

Srand Time Null Dev C++

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.

Dev null c++ download

C++ Is Null

Enjoy!