Fixed error messages

Used perror() instead of plain printf() for error handling.