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
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
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
- 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 two
fprintf()statements and replaced them with
perror()instead. The changes from the previous version are highlighted here: