Using errno with perror()
In the previous recipe, we used strerror()
to get a string containing a human-readable error message from errno
. There's another function similar to strerr()
called perror()
. Its name stands for print error, and that's what it does; it prints the error message directly to stderr.
In this recipe, we'll write the sixth version of our simple touch program. This time, we'll replace both of the fprinf()
lines with perror()
.
Getting ready
The only programs necessary for this recipe are the GCC compiler and the Make tool (along with the generic Makefile).
How to do it…
Follow these steps to create an even shorter and better version of simple-touch
:
- Write the following code into a file and save it as
simple-touch-v6.c
. This time, the program is even smaller. We have removed the twofprintf()
statements and replaced them withperror()
instead. The changes from the previous version are highlighted here:#include <stdio...