On Linux, you can replace your sprintf call with
snprintf (errorMessage, sizeof(errorMessage), "Could not open file %s - %m", fileName);
(General hint to avoid sprintf due to possible buffer overflows and use only snprintf )
If you want to avoid the %m GLibc format specifier, as well as use more standard functions, the code
snprintf (errorMessage, sizeof(errorMessage), "Could not open file %s - %s", fileName, strerror(errno));
and don't forget the #include <errno.h> , and read the errno (3) man page carefully.
By the way, you could avoid executing both snprintf , printf and just code
fprintf (stderr, "Cannot open file %s - %s\n", fileName, strerror(errno));
(error messages are usually sent to stderr , as Jonathan recalled)
Then run your program again. Perhaps you have a problem with character encoding (either in the source file or in the file system).
You can also use strace (and possibly ltrace ) in your program to understand the actual system calls it makes.