This blog is under construction

Tuesday, 20 March 2012

error_at_line

Header file:
    error.h

Synopsis:
       void error_at_line(int status, int errnum, const char *filename,
                          unsigned int linenum, const char *format, ...);
Description:
    It is a glibc error reporting function similar to error.  It prints the program name, filename, line no and the error message as its output.


Sample program for error_at_line in C:



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

  FILE *fp;

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

        if (fp == NULL) {
                /* errno - available in errno.h */
                 lineno = 16;
                error_at_line(status, errno, argv[1], lineno, errmsg);
                printf("Status: %d\n", status);
                return;
        }
        fclose(fp);
        return 0;
  }



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


No comments:

Post a Comment