This blog is under construction

Tuesday 20 March 2012

error

Header file:
    error.h

Synopsis:
    void error(int status, int errnum, const char *format, ...);

Description:
    It is a glibc error reporting function.  It prints the program name followed by colon and the error message.


Sample program for error in C:



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

  FILE *fp;

  int main(int argc, char **argv) {
        char errmsg[256];
        int status;
        fp = fopen(argv[1], "r");

        if (fp == NULL) {
                /* errno - available in errno.h */
                error(status, errno, errmsg);
                printf("Status: %d\n", status);
                return;
        }
        fclose(fp);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/cpgms/chap2$ ls
  a.out  error.c
  jp@jp-VirtualBox:~/cpgms/chap2$ ./a.out jp.txt
  ./a.out:  No such file or directory
  Status: 0

No comments:

Post a Comment