tags:

views:

111

answers:

3

Hello,

I am just testing a small program that I want to test.

I am wondering if there is a way to use the stderr to display what the actual error was.

For example, if the file doesn't exist. Is there a standard error that I can display.

I am using stderr, and I thought by using that, I could display what the actual error was.

For example. If the file doesn't exit. Does any errors get sent to stderr that can be displayed?

I hope I am clear with my question.

Many thanks for any advice.

#include <stdio.h>
#include <string.h>

int main(void)
{
    char buffer[100] = {'\0'}; /* declare and clean buffer */

    FILE *fp;
    int len_of_buff = 0;

    fp = fopen("licenseURL.txt", "r");

    if(fp == NULL)
    {
     fprintf(stderr, "There was a error opening a file ???");

     exit(1);
    }

    fgets(buffer, sizeof(buffer), fp);
    len_of_buff = strlen(buffer);
    buffer[len_of_buff + 1] = '\0'; /* null terminate */
    printf("The url is: [ %s ]\n", buffer);

    fclose(fp);
}
+3  A: 

Use the strerror() function to retrieve a string describing the error.

David Schmitt
+3  A: 

Replace your fprintf(stderr, ...) call with:

perror("file open");

(stderr is just a stream for sending error messages to, so that they don't get mixed up with the normal program output - in case you're redirecting to a file or similar).

caf
+1  A: 
anselm