A question about printing error message with perror

Dear all,

I use perror in order to print an error message to the standar error. For example
if a C program is called without its two necessary command line parameters
then :

if (argc != 3)
{
    perror("use: ./myProgram <source file> <target file>\n");
    return 1;
}

Now the problem is whenever this error message is printed, I have this:

use: ./myProgram <source file> <target file>\n
: Success

What I don't understand is why there is a second line: ":Success"??
Is there a way to remove it? as I want to print an error message, it doesn't
really make sense to print Success.

Thanks in advance,
Kind Regards,
:slight_smile:

Did you read up on what perror() does?

Since neither you nor any other function set errno, it's probably defaulted to zero, which means success.

If you want to output any error messages that stem from logical errors, I'd suggest writing them to stderr via fprintf.

Dear pludi

Thank you very much for your help. Your solution solved my problem.

if (argc != 3)
{
    fprintf(stderr, "use: ./myProgram <source file> <target file>\n");
    return 1;
}

And it prints only the error message.

Thanks a lot for your help.

Kind Regards,
:smiley: