Header file:
string.h
Synopsis:
char *strerror(int errnum);
Description:
Returns a pointer to a string that describes the error code which is passed as argument.
Sample program for strerror in C:
#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);
printf("Error message=> %s\n", errmsg);
return;
}
fclose(fp);
return 0;
}
#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);
printf("Error message=> %s\n", errmsg);
return;
}
fclose(fp);
return 0;
}
Output:
jp@jp-VirtualBox:~/cpgms/chap2$ ls
a.out strerror.c
jp@jp-VirtualBox:~/cpgms/chap2$ ./a.out jp.txt
Error message=> No such file or directory
a.out strerror.c
jp@jp-VirtualBox:~/cpgms/chap2$ ./a.out jp.txt
Error message=> No such file or directory
No comments:
Post a Comment