This blog is under construction

Tuesday 20 March 2012

perror example in C

Header file:
    stdio.h

Synopsis:
    void perror(const char *str);

Description:
     Prints error string str and system error message corresponds to errno.


perror function C example:


  #include<stdio.h>
  #include<string.h>
  #include<errno.h>
  int main(int argc, char **argv) {
        FILE *fp;
        char *errmsg;
        fp = fopen(argv[1], "r");

        if (fp == NULL) {
                /* errno - available in errno.h */
                errmsg = strerror(errno);
                perror(errmsg);
                return;
        }
        fclose(fp);
        return 0;
  }



  Output:
  No such file or directory: No such file or directory




No comments:

Post a Comment